6

Is it possible to disable manual sorting on a JTable after adding a sorter? So I have a JTable that has the following sorter attached to it (basically sorts by column 3 when the table is initialised):

JTable jTable = new JTable();
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(jTable.getModel());
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new RowSorter.SortKey(3, SortOrder.DESCENDING));
sorter.setSortKeys(sortKeys); 
jTable.setRowSorter(sorter);

This works fine, however the user is still able to click on the column headers in the table and sort by any of the columns, which I want to disable. Is this possible?

maloney
  • 1,633
  • 3
  • 26
  • 49
  • 1
    You might like to check @mkorbel's suggests from [this](http://stackoverflow.com/questions/14365510/how-do-i-prevent-a-java-swing-jtable-column-from-being-sorted-when-the-user-clic) similar questions – MadProgrammer Jan 03 '14 at 12:15
  • reading Default/RowSorter's api doc might help :-) – kleopatra Jan 03 '14 at 12:29

4 Answers4

17

You can use the setSortable method of TableRowSorter as below:

sorter.setSortable(0, false); 

to make column 0 non-sortable. You can apply it on the column according to your requirement.

Rahul
  • 3,479
  • 3
  • 16
  • 28
  • 1
    `RowSorter#setSortable(int, boolean)` function does not exist. You might be referring to some other class that has it, but your answer doesn't reference that at all, which makes it a bad answer. Please update. – searchengine27 Aug 30 '18 at 20:47
  • Technically it's more accurate to say `DefaultRowSorter#setSortable(int, boolean)`, but since TableRowSorter extends DefaultRowSorter, this answer is still valid. – searchengine27 Jan 08 '19 at 16:04
  • `((DefaultRowSorter) jTable.getRowSorter()).setSortable(0, false);` would prevent column 0 from being sorted. As stated on the other comments `setSortable(int,bool)` belongs to `DefaultRowSorter` – LeedMx Aug 26 '20 at 16:09
6

Alternatively, you can set your sortable and non-sortable columns like this:

TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel()) {
    @Override
    public boolean isSortable(int column) {
        if(column < 2)
            return true;
        else 
            return false;
    };
};
table.setRowSorter(sorter);
mostar
  • 4,723
  • 2
  • 28
  • 45
6

I ran into the same problem recently, and found the perfect solution to this problem. The TableHeader can simply be disabled.

jTable.getTableHeader().setEnabled(false);

This way, the sorter works perfectly on any of the columns, but manual sorting is prevented by click on the Column Headers.

Hope this helps the future users who might want to have a look at it.

Sandip Ghosh
  • 719
  • 7
  • 13
1

In case your table derived from com.jidesoft.grid.TreeTable (JIDE), you can do this:

 class MyTreeTable extends TreeTable
    {
        public MyTreeTable()
        {
            getSortableTableModel().setColumnSortable(0, false);
        }
    }

Note that getSortableTableModel is protected so it must be called from within a TreeTable method (constructor in this example).

neflow
  • 76
  • 3