gpt4 book ai didi

java - JTable、RowFilter 和 RowFilter.Entry

转载 作者:搜寻专家 更新时间:2023-11-01 01:53:37 25 4
gpt4 key购买 nike

  1. 我对 RowFilter 有疑问( its ComparisonType ) 放在一起

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

    连同

    • 与、或、或非
  2. 不知道怎么办

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

  4. 在此SSCCEframe.add(new JButton(new AbstractAction("Toggle filter") {

  5. 中查看我的基本问题

enter image description here enter 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();
}
});
}
}

最佳答案

我没有看到意外的结果。

  1. 第一个谓词很简单;它包括两个重叠集的交集,其中值 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. 第三个谓词是第一个谓词的补充,具有值 13 的行交替包含和排除。

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

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

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

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

将它们组合在一个andFilter中。

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

结果选择 d1d2 之间但不包括在内的行。

修改后的 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();
}
});
}
}

关于java - JTable、RowFilter 和 RowFilter.Entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17854854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com