tags:

views:

1849

answers:

4
+2  Q: 

JTable Sort

I know that Jtable can sort by a single column. But is it possible to allow for multiple column sort or do i need to write the code myself?

Thanks in advance

+1  A: 

You should be able to set the TableRowSorter and the Comparator associated with it. Example:

TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
TableRowSorter t = new TableRowSorter(myModel);
t.setComparator(column that the comparator works against, Comparator<?> comparator);
table.setRowSorter(new TableRowSorter(myModel));
Joshua
+2  A: 

Look into JXTable. JXTable is an extension of JTable that supports multi-column sorting, as well as other functions that JTable doesn't provide. It's freely available from JDNC / SwingLabs.

David
Are you sure there? Actually the JXTable API states: "Sorting support is single column only."
räph
Ok, but it works with filters. you just have to put shuttlesorters into a filterpipeline
räph
+4  A: 

You can sort by multiple columns by specifying more than one sort key when calling setSortKeys in the RowSorter you're using.

Chris Jester-Young
a pity this isn't available in java5
räph
+1  A: 

ETable from the netbeans collection.
It is part of org-netbeans-swing-outline.jar
A google search aught to turn it up. The ETable is primarily a foundation for Outline (a TreeTable) but it has multi-column ordering built in as well as many other nice features