2

为了对 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);
4

2 回答 2

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

参考资料

于 2013-06-30T15:22:08.563 回答
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();
          }
于 2013-06-30T16:45:37.173 回答