Set the sort order for a table using the specified column given a RowSorter for the TableModel containing the column. - Java Swing

Java examples for Swing:JTable Model

Description

Set the sort order for a table using the specified column given a RowSorter for the TableModel containing the column.

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;
import javax.swing.RowSorter;
import javax.swing.RowSorter.SortKey;
import javax.swing.SortOrder;

import javax.swing.table.TableModel;

public class Main {
    /**//from   w w  w.  j  a v  a2 s  . c o  m
     * Set the sort order for a table using the specified column given a
     * RowSorter for the TableModel containing the column.
     * <p>
     * This makes all other columns unsorted, even if the specified column is
     * also specified to be unsorted.
     *
     * @param rowSorter the sorter
     * @param column    the column index in the model, not the view
     * @param sortOrder the sort order
     */
    public static void setSortOrder(
            @Nonnull RowSorter<? extends TableModel> rowSorter, int column,
            @Nonnull SortOrder sortOrder) {
        List<SortKey> keys = new ArrayList<>();
        if (!sortOrder.equals(SortOrder.UNSORTED)) {
            keys.add(new RowSorter.SortKey(column, sortOrder));
        }
        rowSorter.setSortKeys(keys);
    }
}

Related Tutorials