The way to listen is to listen to the table sorter changes and set the sortKeys of the second table to the same thing:
RowSorterListener l = new RowSorterListener() { @Override public void sorterChanged(RowSorterEvent e) { if (RowSorterEvent.Type.SORT_ORDER_CHANGED == e.getType()) { RowSorter sorter = e.getSource(); otherTable.getRowSorter().setSortKeys(sorter.getSortKeys()); } } }; table.getRowSorter().addRowSorterListener(l);
If you need to keep synchronization in both directions, register a listener for both and add some logic to do nothing when the sort change was called by the listener.
Edit
after writing almost the same comment twice (to the answers to the proposal to sort by model), I decided to add it here
- technically, sorting can be solved as a responsibility, as well as a model or field of view. In the past (mostly discussed) the pros and cons. After that, stick to this solution everywhere in ui dev
- maintaining the index mapping between the model and the view coordinate system is where problems are hidden, anyway
- Swing / X decided to consider this as the responsibility for browsing, clogging any custom type / synchronization based on the model actually fights the system.
source share