简体   繁体   English

如何按行值对列进行排序?

[英]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. 在线上有太多关于如何按列值对JTable的进行排序的示例。

Currently I am doing this by implementing AbstractTableModel and overridding getColumnClass to return the actual class of the data. 目前,我正在通过实现AbstractTableModel并重写getColumnClass来返回数据的实际类来进行此操作。 Then calling setAutoCreateRowSorter . 然后调用setAutoCreateRowSorter

How can I sort the Columns in a JTable by Row values? 如何按行值对JTable中的进行排序?

1) extract all value from TableHeader to the shortable Array (could be method in AbstractTableModel ) 1)提取所有值TableHeader到卖空阵列(可能是方法AbstractTableModel

2) loop inside array and call 2)在数组内部循环并调用

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

3) for all methods that used Model vs View (and vice versa) is required add 3)对于使用模型与视图(反之亦然)的所有方法,必须添加

table/model#convertXxxIndexToXxx

4) for reset you have to create method in AbstractTableModel by calling 4)对于重置,您必须通过调用在AbstractTableModel创建方法

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

5) notice about above two fireTableXxxXxx 5)关于以上两个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. 只需在JTable上定义并设置RowSorter。 Here's a simple tutorial: 这是一个简单的教程:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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