I created TableRowSorterfor JTable, and I managed to sort the table correctly. But is there a way to return the table to its original state before I sorted it? I tried to click on the table header, but it seems that it only sorts the table in ascending or descending order, and it does not return the table back to its original state before sorting. Is it possible to sort the table and return it to its original state? Thank!
TableRowSorter
JTable
The only way is to insert it into its original state and restore it when you fire the "unsort" event.
This way, you will need to keep the original order of the lines in a list or array that you can restore. You can also keep a link to a duplicate of the original DefaultTableModel.
DefaultTableModel
An empty list sortKeysindicates that the view should be unsorted. To restore the original row order, follow these steps:
sortKeys
table.getRowSorter().setSortKeys(null);
More details in the RowSorter spec.
table.setRowSorter( TableRowSorter < > (table.getModel()));
, .
, NullPointer getColumClass. , , .
this.mainTable.setAutoCreateRowSorter(false); this.tableModel.removeAllElements(); this.mainTable.setAutoCreateRowSorter(true); this.mainTable.repaint(); this.mainScrollPane.doLayout();