Adds a row sorter to table with the specified table model. - Java Swing

Java examples for Swing:JTable Model

Description

Adds a row sorter to table with the specified table model.

Demo Code

/*-//from   ww  w.  ja  va  2 s .c o  m
 * Copyright (C) 2007-2014 Erik Larsson
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class Main {
    /**
     * Adds a row sorter to <code>table</code> with the specified table model. Optionally, a list of
     * Comparators can be supplied, one for each column, that specify the correct way of comparing
     * the objects in that column. Null values means the default comparator will be used.<br>
     * <b>Only Java 6+ virtual machines will support this, so check first with isJava6OrHigher() or
     * risk to crash your program.</b>
     *
     * @param table
     * @param tableModel
     * @param defaultSortColumn the column on which to sort on by default.
     * @param columnComparators
     */
    public static void addRowSorter(JTable table,
            DefaultTableModel tableModel, int defaultSortColumn,
            List<Comparator<?>> columnComparators) {

        try {
            final Class<? extends Object> rowSorterClass = Class
                    .forName("javax.swing.RowSorter");
            final Class<? extends Object> tableRowSorterClass = Class
                    .forName("javax.swing.table.TableRowSorter");
            final Method tableRowSorterSetComparatorMethod = tableRowSorterClass
                    .getMethod("setComparator", int.class, Comparator.class);
            final Method tableRowSorterToggleSortOrderMethod = tableRowSorterClass
                    .getMethod("toggleSortOrder", int.class);
            final Object sorter = tableRowSorterClass.getConstructor(
                    TableModel.class).newInstance(tableModel);

            int i = 0;
            for (Comparator<?> c : columnComparators) {
                if (c != null) {
                    tableRowSorterSetComparatorMethod.invoke(sorter, i, c);
                }

                ++i;
            }

            tableRowSorterToggleSortOrderMethod.invoke(sorter,
                    defaultSortColumn);

            Class<? extends JTable> c = table.getClass();
            Method m = c.getMethod("setRowSorter", rowSorterClass);
            m.invoke(table, sorter);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        } catch (InstantiationException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        } catch (InvocationTargetException ex) {
            final Throwable cause = ex.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else {
                throw new RuntimeException(ex);
            }
        }
    }
}

Related Tutorials