use of javax.swing.event.RowSorterListener in project JMRI by JMRI.
the class RowSorterUtil method addSingleSortableColumnListener.
/**
* Add a RowSorterListener to the rowSorter that prevents multiple columns
* from being considered while sorting.
*
* @param rowSorter the sorter to add the listener to
* @return the added listener
* @throws NullPointerException if rowSorter is null
*/
public static RowSorterListener addSingleSortableColumnListener(@Nonnull RowSorter<? extends TableModel> rowSorter) {
Objects.requireNonNull(rowSorter, "rowSorter must be nonnull.");
RowSorterListener listener = new RowSorterListener() {
List<? extends SortKey> priorSortKeys = new ArrayList<>();
@Override
public void sorterChanged(RowSorterEvent e) {
if (e.getType().equals(RowSorterEvent.Type.SORT_ORDER_CHANGED)) {
List<? extends SortKey> newSortKeys = new ArrayList<>(e.getSource().getSortKeys());
newSortKeys.removeAll(priorSortKeys);
if (!newSortKeys.isEmpty()) {
priorSortKeys = newSortKeys;
e.getSource().setSortKeys(priorSortKeys);
e.getSource().allRowsChanged();
}
}
}
};
rowSorter.addRowSorterListener(listener);
return listener;
}
Aggregations