简体   繁体   中英

How can I sort Columns by Row values?

There are [too] many examples online of how to sort the Rows of a JTable by Column values.

Currently I am doing this by implementing AbstractTableModel and overridding getColumnClass to return the actual class of the data. Then calling setAutoCreateRowSorter .

How can I sort the Columns in a JTable by Row values?

1) extract all value from TableHeader to the shortable Array (could be method in AbstractTableModel )

2) loop inside array and call

TableColumn column = table.getColumnModel().moveColumn(columnIndex, newIndex);

3) for all methods that used Model vs View (and vice versa) is required add

table/model#convertXxxIndexToXxx

4) for reset you have to create method in AbstractTableModel by calling

fireTableDataChanged(); or fireTableStructureChanged(); // not sure in this case

5) notice about above two fireTableXxxXxx

This can be a problem if you have added any custom renderers or editor to 
the table as you would lose them (by @camickr)

Just define and set the RowSorter on your JTable. Here's a simple tutorial:

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting

您可以通过单击列标题对值进行排序(如果您要这样)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM