anon
anon

Reputation: 81

JTable prevent all sorting

Is it possible to prevent sorting all together on the JTable? Basically I don't want anything to happen when the user clicks the table header, and for the content to be in a static order.

Upvotes: 2

Views: 2061

Answers (3)

Cristian Babarusi
Cristian Babarusi

Reputation: 1505

The best and simple way to disable sorting when user clicks on any table header columns:

  1. first you need to create a mouse click listener on the table header
  2. inside of it make only mouse click left avaible (with SwingUtilities)
  3. insert this line of code

    yourTableVariable.setRowSorter(null);

Practical example:

yourTableVariable.getTableHeader().addMouseListener(new MouseAdapter() //here you make the click avaible ONLY on Table Header 
    {
        @Override
        public void mouseClicked(MouseEvent arg0) 
        {
            if (SwingUtilities.isLeftMouseButton(arg0)) //here you select the mouse left click action 
            {
                yourTableVariable.setRowSorter(null); //here is disableing the sorting                  
            }
        }
    });

Upvotes: 0

mKorbel
mKorbel

Reputation: 109813

Basically I don't want anything to happen when the user clicks the table header, and for the content to be in a static order.

basically JTable haven't any Sorter, you have to remove codelines

- JTable#setAutoCreateRowSorter(true);

- table.setRowSorter(sorter);

- custom Comparator added as MouseEvent to the JTableHeader

have look at and read JTable tutorial about Sorting and Filtering

Upvotes: 2

sjr
sjr

Reputation: 9875

See the Javadoc:

public void setRowSorter(RowSorter sorter)

Parameters: sorter - the RowSorter; null turns sorting off

Upvotes: 4

Related Questions