2
votes

I managed to search but did not get the answer I wanted. Please allow me writing here.

Let's say I have a Swing JTable, each row has an ID (also other data). I know I can find out the row with a given ID, by implementing a function in tableModel. My question is, if I have an ID, how can I set the row to 'selected' status in JTable? it should be equivalent to 'using mouse to single click on that row'.

It looks there is not a method like 'setRowToSelected(int rowIndex)' in JTable?

1
This question was originally closed as a duplicate of another question. The solution in the other posting was to use table.setRowSelectionInterval(...). Yes, this does set the row selection, but it does not change the cell that has focus (which is what happens when you click on a cell). - camickr

1 Answers

7
votes

it should be equivalent to 'using mouse to single click on that row'.

table.changeSelection(...);

This will cause the row to be selected and the cell selection to change.

The other option that was pointed to in another thread was:

table.setRowSelectionInterval(...);

This will just select the row, but the current cell selection will remain.