0

After looking through the JTable API I didn't see anything about this but I didn't really know what to look for, anyways take a look at this picture:

Single Selected Cell

See how the "Failed" in the Status column has a blue outline around it, indicating that it was the most recently selected/the mouse was over it when the user stopped dragging. I want to either be able to: 1) disallow the JTable to have a single last-selected-element or 2) be able to set it myself. How do I do that?

If it helps, the reason I want either one of those is that I am refreshing the table which means I need to reapply the selected rows, however when I do so this last-selected-element thing gets lost.

Edit: I would actually really rather disallow the JTable from having a "lead selection index" as it is called, how do I do that?

danglingPointer
  • 882
  • 8
  • 32
  • Without testing, I would recommend having a look at [`setCellSelectionEnabled`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#setCellSelectionEnabled(boolean)) – MadProgrammer Jul 18 '15 at 22:26
  • I've tried that, doesn't work. Thanks tho – danglingPointer Jul 18 '15 at 22:37
  • There doesn't seem to be away to do this without providing your own `TableCellRenderer` as show [here](http://stackoverflow.com/questions/18801137/java-jtable-disable-single-cell-selection-border-highlight) – MadProgrammer Jul 18 '15 at 23:35

1 Answers1

0

That cell indicates which cell has focus as indicated by the following methods:

int row = table.getSelectedRow();
int column = table.getSelectedColumn();

I want to either be able to set it myself

You can set the selected cell and selected rows with code like:

table.changeSelection(6, 2, false, false);
ListSelectionModel lsm = table.getSelectionModel();
lsm.setSelectionInterval(3, 6);

or

ListSelectionModel lsm = table.getSelectionModel();
lsm.setSelectionInterval(3, 6);
table.changeSelection(6, 2, false, true);

Edit:

It appears you need to get the row/column from the appropriate selection model:

int row = table.getSelectionModel().getLeadSelectionIndex();
int column = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
camickr
  • 321,443
  • 19
  • 166
  • 288
  • So that does reapply a most recently selected element, however to the incorrect row & column. table.getSelectedRow() and table.getSelectedColumn() aren't returning the _most recently_ selected row and column, they seem to be either returning the first of both or arbitrarily returning one of the selected rows & columns. – danglingPointer Jul 19 '15 at 20:41
  • @danglingPointer, Oops, it looks like those methods return the first cell selected, not the last cell selected - which is actually what the API says :). See edit. – camickr Jul 19 '15 at 21:47