gpt4 book ai didi

java - JTable 日期过滤器未按应有的方式工作

转载 作者:行者123 更新时间:2023-12-02 09:54:09 26 4
gpt4 key购买 nike

我为我的 JTable 创建了一个日期过滤器,但是,一旦我在为过滤器提供起始日期和截止日期后单击过滤器,表中的所有条目都会消失。我的猜测是日期格式不适合

public void filterDates(Date von, Date bis) {
System.out.println(von);
System.out.println(bis);
List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(2);
filters.add(RowFilter.dateFilter(ComparisonType.AFTER, von));
filters.add(RowFilter.dateFilter(ComparisonType.BEFORE, bis));
TableRowSorter<DefaultTableModel> tr2 = new TableRowSorter<DefaultTableModel>(
(DefaultTableModel) table.getModel());
table.setRowSorter(tr2);
tr2.setRowFilter(RowFilter.andFilter(filters));
}

用于为 filterDates 方法提供输入的另外两个方法是:

private Date von() {
String sVon = txtVon.getText();
SimpleDateFormat f1 = new SimpleDateFormat("dd.MM.yy");
Date dVon = new Date();
try {
dVon = (Date) f1.parse(sVon);
System.out.println(dVon);
return dVon;
} catch (ParseException e) {
e.printStackTrace();
return dVon;
}

并且

private Date bis() {
String sBis = txtBis.getText();
SimpleDateFormat f1 = new SimpleDateFormat("dd.MM.yy");
Date dBis = new Date();
try {
dBis = (Date) f1.parse(sBis);
System.out.println(dBis);
return dBis;
} catch (ParseException e) {
e.printStackTrace();
return dBis;
}
}

这就是将起始日期和截止日期赋予过滤器方法的方式:

if (g.getKeyCode() == KeyEvent.VK_ENTER) {
ed.filterDates(von(), bis());

它应该做的是在两个给定日期 von(从)和 bis(到)之间进行过滤

最佳答案

我把这个例子放在一起,看起来效果很好。

一个主要区别是我传递了要检查的索引...

filters.add(RowFilter.dateFilter(ComparisonType.AFTER, von, 2));

在此示例中,该列号为 3

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.RowFilter.ComparisonType;
import javax.swing.RowSorter;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class TestTableSorter {

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

public TestTableSorter() {
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) {
}

try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
final JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel(
new Object[][]{
{"A", 1, sdf.parse("12/05/2000")},
{"B", 2, sdf.parse("12/06/2000")},
{"C", 3, sdf.parse("12/07/2000")},
{"D", 4, sdf.parse("12/08/2000")},
{"E", 5, sdf.parse("12/09/2000")},
{"F", 1, sdf.parse("12/10/2000")},
{"G", 2, sdf.parse("12/11/2000")},
{"H", 3, sdf.parse("12/12/2000")},
{"I", 4, sdf.parse("12/01/1990")},
{"J", 5, sdf.parse("12/02/1990")},
{"K", 1, sdf.parse("12/03/1990")},
{"L", 2, sdf.parse("12/04/1995")},
{"M", 3, sdf.parse("12/05/1995")},
{"N", 4, sdf.parse("12/06/1995")},
{"O", 5, sdf.parse("12/07/1995")},
{"P", 1, sdf.parse("12/08/1980")},
{"Q", 2, sdf.parse("12/09/1980")},
{"R", 3, sdf.parse("12/10/1980")},
{"S", 4, sdf.parse("12/11/1980")},
{"T", 5, sdf.parse("12/12/1980")},
{"U", 1, sdf.parse("12/01/1985")},
{"V", 2, sdf.parse("12/02/1985")},
{"W", 3, sdf.parse("12/03/1985")},
{"X", 4, sdf.parse("12/04/1985")},
{"Y", 5, sdf.parse("12/05/1985")},
{"Z", 1, sdf.parse("12/06/1985")},},
new Object[]{"Name", "Number", "Date"});

table.setModel(model);
table.setAutoCreateRowSorter(true);

JPanel panel = new JPanel(new GridLayout(1, 3));
panel.add(createButton(table));

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception exp) {
exp.printStackTrace();
}
}

protected JButton createButton(JTable table) {
JButton btn = new JButton("Filter");
btn.addActionListener(new FilterAction(table));
return btn;
}

});
}

public class FilterAction implements ActionListener {

private JTable table;

public FilterAction(JTable table) {
this.table = table;
}

public RowSorter<? extends TableModel> filterDates(Date von, Date bis) {
List<RowFilter<Object, Object>> filters = new ArrayList<>(2);
filters.add(RowFilter.dateFilter(ComparisonType.AFTER, von, 2));
filters.add(RowFilter.dateFilter(ComparisonType.BEFORE, bis, 2));
TableRowSorter<DefaultTableModel> tr2 = new TableRowSorter<>((DefaultTableModel) table.getModel());
tr2.setRowFilter(RowFilter.andFilter(filters));
return tr2;
}

@Override
public void actionPerformed(ActionEvent e) {
LocalDate localFrom = LocalDate.of(1995, Month.JANUARY, 1);
LocalDate localTo = LocalDate.of(2000, Month.AUGUST, 1);

Date from = Date.from(localFrom.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
Date to = Date.from(localTo.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());

RowSorter<? extends TableModel> rowSorter = filterDates(from, to);
table.setRowSorter(rowSorter);
}
}
}

如果您仍然遇到问题,那么您应该提供Minimal, Complete, and Verifiable example

关于java - JTable 日期过滤器未按应有的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56119820/

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