This required a two-stage solution:
First, I had the TableSorter sort when changing data, using this instead of autoCreateRowSorter :
sorter = new TableRowSorter<MyTableModel>(m); table.setRowSorter(sorter); sorter.setSortsOnUpdates(true);
Then I had to change the update method to update the whole table. fireTableCellUpdated and fireTableRowsUpdated will only redraw certain rows that have been updated, not the entire table (this means that you will get a duplicate record that changed as soon as it was redrawn. So, I changed
fireTableCellUpdated(row, col);
to
fireTableRowsUpdated(0, data.size() - 1);
and now it is sorted correctly even when the data changes, and the selection is saved.
Paul fisher
source share