tags:

views:

2425

answers:

3

Is there a simple way to sort rows in a JTable with Java 1.5 (setAutoCreateRowSorter and TableRowSorter appear to be Java 1.6 features)?

+1  A: 

Use the JXTable from the SwingX project, see e.g.

Kaarel
A: 

JXTable requires a big package, and is difficult to get the right version for. (no higher than version 1.0 for Java 1.5).

Try instead TableSorter.java. Get it at:

http://ouroborus.org/java/2.1/TableSorter.java

And insert it in your project.

Now you wrap your tableModel in an instance of TableSorter, and instert that into the JTable. TableSorter acts as a "go-between" the JTable-instance and your tableModel.

Use it something like this (code untested):

JTable myTable = new JTable();
TableSorter mySorter = new TableSorter(myTableModel, myTable.getTableHeader());
myTable.setTableModel(mySorter);

You can set sprting programmatically like this:

mySorter.setSortingStatus(0,TableSorter.ASCENDING);

Try tweeking MouseHandler.mouseClicked() to get it to skip the NOT_SORTED option in the clicking order, and mess with renderers for e better column header and placing and visibility of triangle.

Terje Dahl
+1  A: 

Sorting in Java 1.5 is only possible via libraries.

E.g. use the JXTable mentioned from Kaarel or VLTable from here.

Another good library is glazedlists

which is also used in the Spring Rich Client project.

There are even ways to use Glazed Lists with JXTable

Karussell