为了对 JTable 进行排序,我使用了具有不同键的 TableRowSorter,效果很好。但现在我想将此排序器也应用于用户更改排序方向的情况。可能我必须这样做toggleSortOrder
,但我必须在这里做什么?
sorter = new TableRowSorter<TableModel>(model) {
@Override
public void toggleSortOrder(int column) {
List<SortKey> keys = new ArrayList<SortKey>(sorter.getSortKeys());
//use sorter
super.toggleSortOrder(column);
}
};
List<SortKey> keys = new ArrayList<SortKey>();
SortKey sortKey, sortKey2, sortKey3;
if(sortType == 0) {
if(sortDirection == 0) {
sortKey = new SortKey(1, SortOrder.ASCENDING);
sortKey2 = new SortKey(0, SortOrder.ASCENDING);
keys.add(sortKey);
keys.add(sortKey2);
} else {
sortKey = new SortKey(1, SortOrder.DESCENDING);
sortKey2 = new SortKey(0, SortOrder.DESCENDING);
keys.add(sortKey);
keys.add(sortKey2);
}
} else {
if(sortDirection == 0) {
sortKey = new SortKey(2, SortOrder.ASCENDING);
sortKey2 = new SortKey(1, SortOrder.ASCENDING);
sortKey3 = new SortKey(0, SortOrder.ASCENDING);
keys.add(sortKey);
keys.add(sortKey2);
keys.add(sortKey3);
} else {
sortKey = new SortKey(2, SortOrder.DESCENDING);
sortKey2 = new SortKey(1, SortOrder.DESCENDING);
sortKey3 = new SortKey(0, SortOrder.DESCENDING);
keys.add(sortKey);
keys.add(sortKey2);
keys.add(sortKey3);
}
}
sorter.setRowFilter(null);
sorter.setSortKeys(keys);
sorter.sort();
sorter.setSortsOnUpdates(true);
table.setRowSorter(sorter);