Java Utililty Methods JTable Row Sort

List of utility methods to do JTable Row Sort

Description

The list of methods to do JTable Row Sort are organized into topic(s).

Method

RowSorterListeneraddSingleSortableColumnListener( @Nonnull RowSorter rowSorter)
Add a RowSorterListener to the rowSorter that prevents multiple columns from being considered while sorting.
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)) {
            RowSorter<?> source = e.getSource();
            List<? extends SortKey> newSortKeys = new ArrayList<>(source.getSortKeys());
...
voidapplyRowSorter(JTable table)
apply Row Sorter
@SuppressWarnings({ "rawtypes", "unchecked" })
TableRowSorter<?> sorter = new TableRowSorter(table.getModel());
sorter.setSortsOnUpdates(true);
table.setRowSorter(sorter);
TableRowSortercreateTableRowSorter(Class clazz, TableModel model)
create Table Row Sorter
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++) {
...
voidfilt(ArrayList selectedLevel, String text, TableRowSorter sorter)
filt
ArrayList<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(2);
RowFilter<Object, Object> textFilter = null;
RowFilter<Object, Object> levelFilter = null;
if (text.length() != 0) {
    textFilter = RowFilter.regexFilter(text);
if (!selectedLevel.isEmpty()) {
    String levels = "[";
...
SortOrdergetSortOrder(@Nonnull RowSorter rowSorter, int column)
Get the sort order for a column given a RowSorter for the TableModel containing the column.
for (SortKey key : rowSorter.getSortKeys()) {
    if (key.getColumn() == column) {
        return key.getSortOrder();
return SortOrder.UNSORTED;
voidjtable$setAutoCreateRowSorter(JTable table)
jtablset Auto Create Row Sorter
try {
    Method e = table.getClass().getMethod("setAutoCreateRowSorter", new Class[] { Boolean.TYPE });
    e.invoke(table, new Object[] { Boolean.valueOf(true) });
} catch (Exception var2) {
    var2.printStackTrace();
voidsetSortOrder(@Nonnull RowSorter rowSorter, int column, @Nonnull SortOrder sortOrder)
Set the sort order for a table using the specified column given a RowSorter for the TableModel containing the column.
List<SortKey> keys = new ArrayList<>();
if (!sortOrder.equals(SortOrder.UNSORTED)) {
    keys.add(new RowSorter.SortKey(column, sortOrder));
rowSorter.setSortKeys(keys);
voidtryToEnableRowSorting(JTable listTbl)
try To Enable Row Sorting
TableRowSorter sorter = new TableRowSorter(listTbl.getModel());
listTbl.setRowSorter(sorter);