How to speed up the JTable built into table sorting?

I have a JTable with a custom TableModel that extends AbstractTableModel. I also used the built-in table sort by calling:

table.setAutoCreateRowSorter(true);

The model also returns the correct data class for each column from the getColumnClass () call, which from what I read should provide the fastest sorting.

While it works fine and a really really fast sorting method in JTables, it is exceptionally slow when the number of rows reaches 5000+ records. My nearly 10,000 row table now takes 6-7 seconds to sort on a fairly powerful computer. But if I sort the data myself before adding it to the model using the collection sorting algorithm, this is done in a few milliseconds!

I suspect that the built-in sorter fires a lot of unnecessary events for each "swap" of elements that go into the sorter algorithm, even if the repainting is stopped before it finishes (the built-in sorter obviously works in the AWT stream and, therefore, blocks the entire GUI / repainting). I did not analyze this by looking at what actually happens in the sorting of the table.

I am tempted to abandon the entire built-in sorter and simply detect clicks on the column header and just sort the model myself before doing fireTableDataChanged (), which was what the built-in sorter should do.

But before I do this, I don’t notice what the built-in sorter can make fast?

+3
2

:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableSort extends JFrame
{
    JTable table;
    DefaultTableModel model;

    public TableSort()
    {
        Random random = new Random();
        model = new DefaultTableModel(0, 2)
        {
            public Class getColumnClass(int column)
            {
                return Integer.class;
            }
        };

        for (int i = 0; i < 10000; i++)
        {
            Integer[] row = new Integer[2];
            row[0] = random.nextInt(100000);
            row[1] = random.nextInt(100000);
            model.addRow( row );
        }

        table = new JTable( model );
        table.setAutoCreateRowSorter(true);
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
        System.out.println(table.getRowCount());
    }

    public static void main(String[] args)
    {
        TableSort frame = new TableSort();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}

?

0

GlazedLists? JTable .

+1

Source: https://habr.com/ru/post/1766189/


All Articles