java JTable、RowFilter 和 RowFilter.Entry

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17854854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 17:48:44  来源:igfitidea点击:

JTable, RowFilter and RowFilter.Entry

javaswingjtablerowfilter

提问by mKorbel

  1. please I have an issue with RowFilter(its ComparisonType) to put together

    • Regex(String), Number, Date

    together with

    • AND, OR, NOR
  2. no idea how

    • to handle with null value for AND, OR, NOR
    • if Regex(String), Number, Date has the same logics or there "small differencies"
    • especially how to compare Date value
  3. some longer and detailed descriptions are welcome

  4. in this SSCCEto see my basic issues into frame.add(new JButton(new AbstractAction("Toggle filter") {

  1. 请我对RowFilter它的比较类型)有一个问题要放在一起

    • 正则表达式(字符串)、数字、日期

    和...一起

    • 与、或、或非
  2. 不知道如何

    • 处理 AND、OR、NOR 的空值
    • 如果 Regex(String)、Number、Date 具有相同的逻辑或存在“小差异”
    • 特别是如何比较日期值
  3. 欢迎提供更长更详细的描述

  4. 在这个SSCCE中查看我的基本问题frame.add(new JButton(new AbstractAction("Toggle filter") {

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class JTableFilterDemo {

    private static TableRowSorter<TableModel> sorter;
    private Object[][] data = {{"A", 5, true, new Date()},
        {"B", 2, false, new Date()}, {"C", 4, false, new Date()},
        {"D", 8, true, new Date()}, {"E", 13, false, new Date()},
        {"F", 7, true, new Date()}, {"G", 55, false, new Date()},
        {"H", 6, false, new Date()}, {"I", 1, true, new Date()}};
    private String columnNames[] = {"Item", "Value", "Boolean", "Date"};
    private TableModel model = new DefaultTableModel(data, columnNames) {
        private static final long serialVersionUID = 1L;

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case 1:
                    return Integer.class;
                case 2:
                    return Boolean.class;
                case 3:
                    return Date.class;
                default:
                    return String.class;
            }
        }
    };
    private JTable table = new JTable(model);
    private Date modifDate = new Date();

    public JTableFilterDemo() {
        modifyDateInTable();
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        RowFilter<Object, Number> filter = new RowFilter<Object, Number>() {
            @Override
            public boolean include(RowFilter.Entry entry) {
                //String str = (String) entry.getValue(0);//String
                //return str.matches(("(?i)^a|^g"));//String
                //Boolean bol = (Boolean) entry.getValue(2);//Boolean
                //return bol.booleanValue() == false;//Boolean
                //Date date = (Date) entry.getValue(3);//Date
                //return date.getTime() > (long) (new Date().getTime());//Date
                //return ((Number) entry.getValue(1)).intValue() % 2 == 0;//Integer
                //return ((Number) entry.getValue(1)).intValue() > 0;//Integer
                return ((Number) entry.getValue(1)).intValue() > 10
                        & ((Number) entry.getValue(1)).intValue() < 50;//AND with Integer
            }
        };
        sorter = new TableRowSorter<TableModel>(model);
        sorter.setRowFilter(filter);
        table.setRowSorter(sorter);
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Filtering Table");
        frame.add(new JButton(new AbstractAction("Toggle filter") {
            private static final long serialVersionUID = 1L;
            private RowFilter<TableModel, Object> filter = new RowFilter<TableModel, Object>() {
                @Override
                public boolean include(javax.swing.RowFilter.Entry<? extends TableModel, ? extends Object> entry) {
                    //String str = (String) entry.getValue(0);//String
                    //return str.matches(("(?i)^a|^g"));//String
                    //Boolean bol = (Boolean) entry.getValue(2);//Boolean
                    //return bol.booleanValue() == false;//Boolean
                    //Date date = (Date) entry.getValue(3);//Date
                    //return date.getTime() > (long) (new Date().getTime());//Date
                    //return ((Number) entry.getValue(1)).intValue() % 2 == 0;//Integer
                    //return ((Number) entry.getValue(1)).intValue() > 0;//Integer
                    return ((Number) entry.getValue(1)).intValue() > 10
                            & ((Number) entry.getValue(1)).intValue() < 50;//AND with Integer

                    // --->  doesn't works
                    //return ((Number) entry.getValue(1)).intValue() > 10 |
                    //((Number) entry.getValue(1)).intValue() < 50;//OR with Integer

                    // ---> works, but initialized on 2nd. event
                    //return ((Number) entry.getValue(1)).intValue() > 10 != 
                    //((Number) entry.getValue(1)).intValue() < 50;//NOR with Integer 
                }
            };

            @Override
            public void actionPerformed(ActionEvent e) {
                if (sorter.getRowFilter() != null) {
                    sorter.setRowFilter(null);
                } else {
                    sorter.setRowFilter(filter);
                }
            }
        }), BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }

    private void modifyDateInTable() {
        Calendar c = Calendar.getInstance();
        c.setTime(modifDate);
        c.add(Calendar.DATE, - 1);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 0, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +5);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 1, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +1);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 2, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, - 16);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 3, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +30);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 4, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +55);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 5, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +155);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 6, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, -23);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 7, 3);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JTableFilterDemo jtfd = new JTableFilterDemo();
            }
        });
    }
}

回答by trashgod

I see no unexpected result.

我看不出意外的结果。

  1. The first predicate is straightforward; it includes the intersectionof two overlapping sets, with the row having Value 13as the only member.

    return ((Number) entry.getValue(1)).intValue() > 10
        & ((Number) entry.getValue(1)).intValue() < 50;
    
  2. The second predicate is the unionof two overlapping sets, with all rows included.

    return ((Number) entry.getValue(1)).intValue() > 10 |
        ((Number) entry.getValue(1)).intValue() < 50;
    
  3. The third predicate is the complementof the first, with the row having Value 13alternately included and excluded.

    return ((Number) entry.getValue(1)).intValue() > 10 != 
        ((Number) entry.getValue(1)).intValue() < 50;
    
  1. 第一个谓词很简单;它包括两个重叠集的交集,行Value 13作为唯一成员。

    return ((Number) entry.getValue(1)).intValue() > 10
        & ((Number) entry.getValue(1)).intValue() < 50;
    
  2. 第二谓词是联合的两个重叠的集合,与包括所有行。

    return ((Number) entry.getValue(1)).intValue() > 10 |
        ((Number) entry.getValue(1)).intValue() < 50;
    
  3. 第三个谓词是第一个谓词的补语,该行Value 13交替包含和排除。

    return ((Number) entry.getValue(1)).intValue() > 10 != 
        ((Number) entry.getValue(1)).intValue() < 50;
    

Addendum: Regarding RowFilter.Entry, I found it helpful to examine how the and, orand notfactories use the extra indexparameter defined in the private class RowFilter.GeneralFilter; each subclass invokes the parent's implementation of include()for each filter supplied in the constructor. In contrast, the date, numberand regexfactories do not; instead, each relies on predicates available to the underlying type.

附录:关于RowFilter.Entry,我发现它有助于研究如何没有工厂使用额外index的私有类定义的参数RowFilter.GeneralFilter; 每个子类include()为构造函数中提供的每个过滤器调用父类的实现。相比之下,datenumberregex工厂没有;相反,每个都依赖于可用于基础类型的谓词。

Addendum: As a concrete example, select two model dates, e.g. the first and penultimate.

附录:作为一个具体的例子,选择两个模型日期,例如第一个和倒数第二个。

Date d1 = (Date) model.getValueAt(0, 3);
Date d2 = (Date) model.getValueAt(model.getRowCount() - 2, 3);

Create two filters that bracket the chosen dates.

创建两个将所选日期括起来的过滤器。

RowFilter<TableModel, Integer> low = 
    RowFilter.dateFilter(RowFilter.ComparisonType.AFTER, d1, 3);
RowFilter<TableModel, Integer> high = 
    RowFilter.dateFilter(RowFilter.ComparisonType.BEFORE, d2, 3);

Combine them in an andFilter.

将它们组合在一个andFilter.

List<RowFilter<TableModel, Integer>> filters = Arrays.asList(low, high);
final RowFilter<TableModel, Integer> filter = RowFilter.andFilter(filters);

The result selects rows between, but excluding, d1and d2.

结果选择介于但不包括d1和之间的行d2

Modified SSCCE:

修改后的 SSCCE:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class JTableFilterDemo {

    private static TableRowSorter<TableModel> sorter;
    private Object[][] data = {{"A", 5, true, new Date()},
        {"B", 2, false, new Date()}, {"C", 4, false, new Date()},
        {"D", 8, true, new Date()}, {"E", 13, false, new Date()},
        {"F", 7, true, new Date()}, {"G", 55, false, new Date()},
        {"H", 6, false, new Date()}, {"I", 1, true, new Date()}};
    private String columnNames[] = {"Item", "Value", "Boolean", "Date"};
    private TableModel model = new DefaultTableModel(data, columnNames) {
        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case 1:
                    return Integer.class;
                case 2:
                    return Boolean.class;
                case 3:
                    return Date.class;
                default:
                    return String.class;
            }
        }
    };
    private JTable table = new JTable(model);

    public JTableFilterDemo() {
        modifyDateInTable();
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        sorter = new TableRowSorter<TableModel>(model);
        table.setRowSorter(sorter);
        Date d1 = (Date) model.getValueAt(0, 3);
        Date d2 = (Date) model.getValueAt(model.getRowCount() - 2, 3);
        RowFilter<TableModel, Integer> low = RowFilter.dateFilter(RowFilter.ComparisonType.AFTER, d1, 3);
        RowFilter<TableModel, Integer> high = RowFilter.dateFilter(RowFilter.ComparisonType.BEFORE, d2, 3);
        List<RowFilter<TableModel, Integer>> filters = Arrays.asList(low, high);
        final RowFilter<TableModel, Integer> filter = RowFilter.andFilter(filters);
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Filtering Table");
        frame.add(new JButton(new AbstractAction("Toggle filter") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sorter.getRowFilter() != null) {
                    sorter.setRowFilter(null);
                } else {
                    sorter.setRowFilter(filter);
                }
            }
        }), BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void modifyDateInTable() {
        Date modifDate = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(modifDate);
        c.add(Calendar.DATE, - 1);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 0, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +5);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 1, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +1);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 2, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, - 16);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 3, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +30);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 4, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +55);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 5, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, +155);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 6, 3);
        c.setTime(modifDate);
        c.add(Calendar.DATE, -23);
        modifDate = c.getTime();
        table.setValueAt(modifDate, 7, 3);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JTableFilterDemo jtfd = new JTableFilterDemo();
            }
        });
    }
}