Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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));

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

"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? "

Table sorting and filtering is managed by a sorter object. The easiest way to provide a sorter object is to set autoCreateRowSorter bound property to true;

JTable table = new JTable();
table.setAutoCreateRowSorter(true);

This action defines a row sorter that is an instance of javax.swing.table.TableRowSorter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top