How can I find out which row in a JTable the user just clicked?
Asked
Active
Viewed 2.9k times
2 Answers
34
Try this:
aJTable.rowAtPoint(evt.getPoint());

mtmurdock
- 12,756
- 21
- 65
- 108

Paul Tomblin
- 179,021
- 58
- 319
- 408
13
If you only ever care about listening to selections on the JTable:
jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int sel = jTable.getSelectedRow();
}
});

oxbow_lakes
- 133,303
- 56
- 317
- 449
-
Does this still work if it's in column or cell selection mode? – Paul Tomblin Oct 29 '08 at 12:29
-
It should, but it also allows to more more with in reaction to the click. The "answer" will need some kind of listener so you know when to read the value. – javydreamercsw Jul 12 '12 at 22:04