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.
We can sort a JTable in a particular column by using the method setAutoCreateRowSorter() and set to true of JTable class.
setReorderingAllowed() method and set the value as false.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With