Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to track column selections in JTable?

Status
Not open for further replies.

Strannik

Programmer
Jul 4, 2002
132
UA
Is there any way to track column selections for JTable ?valueChanged event of ListSelectionListener interface is very usefull but I can't know whether column selection has changed for current row ...
 
In general I need to allow user to select rows only not single cells. So if somebody can help ...
 
You can just put a MouseListener on the table and get the selected row:

Code:
jTable1.addMouseListener(new MouseAdapter()
{
   public void mouseClicked(MouseEvent e)
   {
      jTable1_mouseClicked(e);
   }
});

private void jTable1_mouseClicked(MouseEvent me)
{
   if (me.getClickCount() > 1)  // single click
   {
      int row = jTable1.getSelectedRow();
   }
}
 
Create a new class that extends DefaultTableCellRenderer and override its getTableCellRendererComponent method,
Code:
class NoSelectionBorderCellRenderer extends DefaultTableCellRenderer {
	public Component getTableCellRendererComponent(<parameters>) {
		Component comp = super.getTableCellRendererComponent(<parameters>);
		setBorder(noFocusBorder);
		return comp;
	}
}
then set it as the cell default renderer with
Code:
yourTable.setDefaultRenderer(Object.class, new NoSelectionBorderCellRenderer());
To select only a single row at a time, call
Code:
yourTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Hope this helps ;-)
 
to jurchi and idarke:

Thanks a lot.
I'm novice in Java and even coudn't think that such an easy action as set "row selection only" mode could require additional processing ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top