Java Swing JTable select programmatically multiple rows

To select just one row, pass it as both the start and end index:

table.setRowSelectionInterval(18, 18);

Or, if you want to select multiple, non-contiguous indices:

ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
model.addSelectionInterval(1, 1);
model.addSelectionInterval(18, 18);
model.addSelectionInterval(23, 23);

Alternately, you may find that implementing your own subclass of ListSelectionModel and using it to track selection on both the table and the scatterplot is a cleaner solution, rather than listening on the scatterplot and forcing the table to match.


It also works without using the ListSelectionModel:

table.clearSelection();
table.addRowSelectionInterval(1, 1);
table.addRowSelectionInterval(15, 15);
table.addRowSelectionInterval(28, 28);
...

Just don't call the setRowSelectionInterval, as it always clears the current selection before.