JTable Sort Rows基于属性而非表格 [英] JTable Sort Rows based on attribute not in table

查看:206
本文介绍了JTable Sort Rows基于属性而非表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个简单的排序函数:

I wrote this simple sorting function :

public void applyFilter(String filter, int col)
{
    if(filter.length() == 0)
        sorter.setRowFilter(null);

    RowFilter<Object, Object> rf = null;
    try 
    {
        rf = RowFilter.regexFilter(filter, col);
    } 
    catch (java.util.regex.PatternSyntaxException e) 
    {
        return;
    }

    sorter.setRowFilter(rf);
    refreshTable();
}

然后我意识到我想基于我的id过滤表格没有显示到视图(所以它没有它的列)。 java过滤器基于列,但如何根据未显示的其他属性过滤表?我可以轻松地获取行所代表的底层对象并获取id,但是如何根据它进行过滤?

But then I realized I wanted to filter the table based on an id that I haven't shown to the view(So it doesn't have it's column). The java filter is based on a column, but how can I filter the table based on other attributes that aren't displayed? I can easily fetch the underlying object represented by the row and get the id, but how do I go about filtering based on that?

推荐答案

这很大程度上取决于你的数据建模方式,但基本上你可以编写自己的过滤器...

This is going to depend a lot on how your data is modeled, but basically you can write your own filter...

这是我的过滤器实现在我的例子中使用...

This is the filter implementation I've used in my example...

    public class IDFilter extends RowFilter<MyModel, Integer> {

        private int id;

        public IDFilter(int id) {
            this.id = id;
        }

        public boolean include(RowFilter.Entry<? extends MyModel, ? extends Integer> entry) {
            MyModel personModel = entry.getModel();
            RowValue value = personModel.getValueAt(entry.getIdentifier());
            if (value.getId() == id) {
                return true;
            }
            return false;
        }

    }

这是一个运行它的例子...

Here is a example of it running...

public class TestTableFilter {

    public static void main(String[] args) {
        new TestTableFilter();
    }

    public TestTableFilter() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FilterPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class FilterPane extends JPanel {

        private JTable table;
        private int filter;
        private TableRowSorter<MyModel> sorter;

        public FilterPane() {
            setLayout(new BorderLayout());
            MyModel model = new MyModel();
            sorter = new TableRowSorter<MyModel>(model);
            table = new JTable(model);
            table.setRowSorter(sorter);
            add(new JScrollPane(table));

            JButton filterButton = new JButton("Filter");
            filterButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    filter++;
                    System.out.println(filter);
                    RowFilter<MyModel, Integer> rowFilter = null;
                    switch (filter) {
                        case 1:
                            rowFilter = new IDFilter(0);
                            break;
                        case 2:
                            rowFilter = new IDFilter(1);
                            break;
                        default:
                            filter = 0;
                    }
                    sorter.setRowFilter(rowFilter);
                }
            });
            add(filterButton, BorderLayout.SOUTH);
        }

    }

    public class MyModel extends AbstractTableModel {

        private List<RowValue> values;

        public MyModel() {
            values = new ArrayList<>(25);
            for (int index = 0; index < 10; index++) {
                values.add(new RowValue(index % 2, Character.toString((char) (65 + index)), index));
            }
        }

        @Override
        public int getRowCount() {
            return values.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            RowValue row = values.get(rowIndex);
            Object value = null;
            switch (columnIndex) {
                case 0:
                    value = row.getName();
                    break;
                case 1:
                    value = row.getValue();
                    break;
            }
            return value;
        }

        public RowValue getValueAt(int row) {
            return values.get(row);
        }

    }

    public class RowValue {

        private int id;
        private String name;
        private int value;

        public RowValue(int id, String name, int value) {
            this.id = id;
            this.name = name;
            this.value = value;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public int getValue() {
            return value;
        }

    }

    public class IDFilter extends RowFilter<MyModel, Integer> {

        private int id;

        public IDFilter(int id) {
            this.id = id;
        }

        public boolean include(RowFilter.Entry<? extends MyModel, ? extends Integer> entry) {
            MyModel personModel = entry.getModel();
            RowValue value = personModel.getValueAt(entry.getIdentifier());
            if (value.getId() == id) {
                return true;
            }
            return false;
        }

    }

}

这篇关于JTable Sort Rows基于属性而非表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆