为了对JTable进行排序,我使用具有不同键的TableRowSorter,效果很好。但是现在我想将此分拣机应用于用户更改排序方向的情况。可能我必须在toggleSortOrder
中这样做,但我必须在这做什么?
sorter = new TableRowSorter<TableModel>(model) {
@Override
public void toggleSortOrder(int column) {
List<SortKey> keys = new ArrayList<SortKey>(sorter.getSortKeys());
//use sorter
super.toggleSortOrder(column);
}
};
List<SortKey> keys = new ArrayList<SortKey>();
SortKey sortKey, sortKey2, sortKey3;
if(sortType == 0) {
if(sortDirection == 0) {
sortKey = new SortKey(1, SortOrder.ASCENDING);
sortKey2 = new SortKey(0, SortOrder.ASCENDING);
keys.add(sortKey);
keys.add(sortKey2);
} else {
sortKey = new SortKey(1, SortOrder.DESCENDING);
sortKey2 = new SortKey(0, SortOrder.DESCENDING);
keys.add(sortKey);
keys.add(sortKey2);
}
} else {
if(sortDirection == 0) {
sortKey = new SortKey(2, SortOrder.ASCENDING);
sortKey2 = new SortKey(1, SortOrder.ASCENDING);
sortKey3 = new SortKey(0, SortOrder.ASCENDING);
keys.add(sortKey);
keys.add(sortKey2);
keys.add(sortKey3);
} else {
sortKey = new SortKey(2, SortOrder.DESCENDING);
sortKey2 = new SortKey(1, SortOrder.DESCENDING);
sortKey3 = new SortKey(0, SortOrder.DESCENDING);
keys.add(sortKey);
keys.add(sortKey2);
keys.add(sortKey3);
}
}
sorter.setRowFilter(null);
sorter.setSortKeys(keys);
sorter.sort();
sorter.setSortsOnUpdates(true);
table.setRowSorter(sorter);
答案 0 :(得分:3)
我不确定为什么简单JTable排序的所有代码都要切换。
见下文:
启动时:
Foo 列标题点击两次后 - 一次用于焦点(显示三角形显示方向)第二次更改排序顺序:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class Test {
public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Object rows[][] = {{"A", 1}, {"A", 2}, {"B", 1}, {"B", 2}, {"C", 1}, {"C", 2}, {"D", 4}, {"E", 5},
{"F", 6}};
String columns[] = {"Foo", "Bar"};
TableModel model = new DefaultTableModel(rows, columns) {
@Override
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
JTable table = new JTable(model);
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane pane = new JScrollPane(table);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
});
}
public static void main(String[] args) {
new Test();
}
}
<强>参考强>:
答案 1 :(得分:0)
为了完整我的解决方案:
@Override
public void toggleSortOrder(int column) {
List<? extends SortKey> sortKeys = getSortKeys();
List<SortKey> newKeys = new ArrayList<SortKey>();
if (sortKeys.size() > 0) {
if(column == 2) {
if (sortKeys.get(0).getSortOrder() == SortOrder.DESCENDING) {
newKeys.add(new SortKey(2, SortOrder.ASCENDING));
newKeys.add(new SortKey(1, SortOrder.ASCENDING));
newKeys.add(new SortKey(0, SortOrder.ASCENDING));
table.getRowSorter().setSortKeys(sortKeys);
} else {
newKeys.add(new SortKey(2, SortOrder.DESCENDING));
newKeys.add(new SortKey(1, SortOrder.DESCENDING));
newKeys.add(new SortKey(0, SortOrder.DESCENDING));
table.getRowSorter().setSortKeys(sortKeys);
}
} else if(column == 1) {
if (sortKeys.get(0).getSortOrder() == SortOrder.DESCENDING) {
newKeys.add(new SortKey(1, SortOrder.ASCENDING));
newKeys.add(new SortKey(0, SortOrder.ASCENDING));
table.getRowSorter().setSortKeys(sortKeys);
} else {
newKeys.add(new SortKey(1, SortOrder.DESCENDING));
newKeys.add(new SortKey(0, SortOrder.DESCENDING));
table.getRowSorter().setSortKeys(sortKeys);
}
}
}
sorter.setSortKeys(newKeys);
sorter.sort();
}