Search in sources :

Example 1 with RowSorterListener

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;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SortKey(javax.swing.RowSorter.SortKey) RowSorterEvent(javax.swing.event.RowSorterEvent) RowSorterListener(javax.swing.event.RowSorterListener)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 SortKey (javax.swing.RowSorter.SortKey)1 RowSorterEvent (javax.swing.event.RowSorterEvent)1 RowSorterListener (javax.swing.event.RowSorterListener)1