Java JTable Row Sort createTableRowSorter(Class clazz, TableModel model)

Here you can find the source of createTableRowSorter(Class clazz, TableModel model)

Description

create Table Row Sorter

License

Open Source License

Declaration

public static final <T extends Comparable<T>> TableRowSorter<TableModel> createTableRowSorter(Class<T> clazz,
            TableModel model) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Comparator;

import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Main {
    public static final <T extends Comparable<T>> TableRowSorter<TableModel> createTableRowSorter(Class<T> clazz,
            TableModel model) {//  www.  j a  v  a2  s  .com
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);

        final Comparator<T> comp = new Comparator<T>() {
            @Override
            public int compare(T o1, T o2) {
                return o1.compareTo(o2);
            }
        };

        for (int i = 0; i < model.getColumnCount(); i++) {
            sorter.setComparator(i, comp);
        }

        return sorter;
    }

    public static final <T extends Comparable<T>> TableRowSorter<TableModel> createTableRowSorter(Class<T> clazz,
            TableModel model, Object... columnMap) {
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);

        final Comparator<T> comp = new Comparator<T>() {
            @Override
            public int compare(T o1, T o2) {
                return o1.compareTo(o2);
            }
        };

        final Comparator<T> compInverse = new Comparator<T>() {
            @Override
            public int compare(T o1, T o2) {
                return o2.compareTo(o1);
            }
        };

        for (int i = 0; i < columnMap.length; i++) {
            final boolean invert = (boolean) columnMap[i + 1];

            sorter.setComparator((int) columnMap[i], invert ? compInverse : comp);
        }

        return sorter;
    }
}

Related

  1. addSingleSortableColumnListener( @Nonnull RowSorter rowSorter)
  2. applyRowSorter(JTable table)
  3. filt(ArrayList selectedLevel, String text, TableRowSorter sorter)
  4. getSortOrder(@Nonnull RowSorter rowSorter, int column)
  5. jtable$setAutoCreateRowSorter(JTable table)
  6. setSortOrder(@Nonnull RowSorter rowSorter, int column, @Nonnull SortOrder sortOrder)