I\'ve figured out how to get a JTable
to be sorted properly, but I can\'t figure out how to get it to automatically update the sort order when a table cell is c
This took a two-step solution:
First I had the TableSorter sort on data change, by using this rather than autoCreateRowSorter
:
sorter = new TableRowSorter(m);
table.setRowSorter(sorter);
sorter.setSortsOnUpdates(true);
Then, I had to change the update method to update the entire table. The fireTableCellUpdated
and the fireTableRowsUpdated
would only redraw the specific rows that were updated, not the entire table (meaning you'd get a duplicate-appearing entry that changed as soon as it was redrawn later. So, I changed
fireTableCellUpdated(row, col);
to
fireTableRowsUpdated(0, data.size() - 1);
and now it sorts properly, even upon data changes, and selection is preserved.