Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you disable all sorting code in JTable in 1.6

I have a JTable extension that has been in use since Java 1.3/1.4 in the project that provided things like column reordering and sorting by clicking on the column. We are upgrading to Java 1.6, and the new JTable stops the old sorting code from working. It would be somewhat extensive rework to fit everything to the new JTable API. Until then is there a way to completely disable those additions in JTable?

Edit: After the further investigation, the problem is centered around the fact that the mouse events on the header are swallowed by Swing in 1.6, and not passed on to the table implementation, even though it sets its own header rendered. So much for vaunted Java backwards compatibility.

So is there a way to get JTable 1.6 to stop? I haven't been able to. Even overriding the UI on the table and the table header didn't help.

like image 909
Yishai Avatar asked Jul 22 '10 20:07

Yishai


People also ask

How to sort values in JTable?

We can sort a JTable in a particular column by using the method setAutoCreateRowSorter() and set to true of JTable class.

How do you make a JTable column not movable?

setReorderingAllowed() method and set the value as false.

What is TableRowSorter in java?

TableRowSorter uses Comparator s for doing comparisons. The following defines how a Comparator is chosen for a column: If a Comparator has been specified for the column by the setComparator method, use it. If the column class as returned by getColumnClass is String , use the Comparator returned by Collator.


2 Answers

Have you tried JTable.setRowSorter(null) ?

edit : and setAutoCreateRowSorter ? (1. create table, 2. row sorter to null, 3. autocreate sorter to false, 4. set model).

like image 82
Istao Avatar answered Sep 21 '22 02:09

Istao


I use this in my JTable subclass and it catches mouse events just fine:

class QueueTable extends JTable {
    public QueueTable() {
        ...
        getTableHeader().addMouseListener(new SortColumnListener(1));
    }
}

The SortColumnListener is implemented like so:

class SortColumnListener extends MouseAdapter {
    SortColumnListener(int column) { ... }

    public void mouseClicked(MouseEvent e) {
        TableColumnModel colModel = QueueTable.this.getColumnModel();
        int columnModelIndex = colModel.getColumnIndexAtX(e.getX());

        if(columnModelIndex == column) {
            // Do stuff
        }
    }
}

This catches mouse events in the SortColumnListener just fine and I can do whatever I want with those events. I have no RowSorter implementation set on the JTable and this works perfectly in Java 5 and Java 6.

For full sourcecode of this class, see: QueueTable.java

like image 34
Gerco Dries Avatar answered Sep 21 '22 02:09

Gerco Dries