aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable - selecting a row after a right click from the mouse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable - selecting a row after a right click from the mouse" Watch "JTable - selecting a row after a right click from the mouse" New topic
Author

JTable - selecting a row after a right click from the mouse

Leonardo Penha
Ranch Hand

Joined: Feb 15, 2002
Posts: 68
How can I select a row from a JTable when I right-click on it?


Leonardo Penha<br />SCJP, SCJD, SCWCD, SCEA, SCMAD, SCDJWS
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
When a user right-clicks you know the position of the right-click (e.getX(), e.getY()).
int row = e.getY()/table.rowHeight;
The selected row can be found by the above formula. There is also one more way to find the selected row. You can use rowAtPoint() method in JTable to find the selected row.
After doing this, you can set the selected row by
doing
table.setRowSelectionInterval(row,row);
Here table is an instance of JTable.
Hope it helps. If you have any problems here lemme know.
Thanks,
- Raja.


SCJP
netharam ram
Ranch Hand

Joined: Aug 09, 2001
Posts: 202
Hi, can this be done in a JList?
Happy middling with java.
Netharam.
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Even JList has setSelectedIndex() and setSelectionInterval() methods. Refer to API documentation on JList for more explanation.
- Raja.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable - selecting a row after a right click from the mouse