Does anyone know if (and how) one can sort a JTable without requiring the user to click on Column headers.
I would like them to click a sort button.
I cant seem to find a fireSort type of method in TableRowSorter.
Any ideas?
Thank you.
Printable View
Does anyone know if (and how) one can sort a JTable without requiring the user to click on Column headers.
I would like them to click a sort button.
I cant seem to find a fireSort type of method in TableRowSorter.
Any ideas?
Thank you.
When the button is pressed then simply sort the data that is contained in that table and put it back in the table model.
How about if you implement your own custom TableModel (extend the stanard one) ? such as when the JTable was loaded, set it's tableModel to your own one, and have a reference to the table model visible to the button's action handler. Where in the custom table model you would have the reorder the data according to the new sort rule.
Thanks i might be able to implement that. After i sort the data and put it back do i have to "update" the table that is displayed? how do i put the data "back" and how do i "update" if i have to?
Thank you!
How about if you implement your own custom TableModel (extend the stanard one) ? such as when the JTable was loaded, set it's tableModel to your own one, and have a reference to the table model visible to the button's action handler. Where in the custom table model you would have the reorder the data according to the new sort rule.
If the above does not work ill try that, good thinking!!
I don't think that this is an either/or situation. You should sort your data as suggested by mrmatt and create your own table model to hold this data as suggested by pharaoh travishein. Often the best class to extend here is the DefaultTableModel class. Best of luck.
Not sure I understand the requirement. Once they click the button to sort the data, how do they unsort the data?Quote:
I would like them to click a sort button.
You can use the following, but the user can still click on the header to sort as well.Quote:
I cant seem to find a fireSort type of method in TableRowSorter.
I know you can disable sorting on specific columns. I don't know if that applies to just clicking on the header or by sorting manually as well.Code:table.setAutoCreateRowSorter(true);
DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(0, SortOrder.ASCENDING) );
sorter.setSortKeys(list);
sorter.sort();