简体   繁体   English

JTable在两个日期之间的rowfilter,同一列

[英]JTable rowfilter between two dates, same column

How can I implement a RowFilter between two dates? 如何在两个日期之间实现RowFilter The dates are in string format. 日期为字符串格式。 Is it necessary to change the format to a date format to apply a RegexFilter ? 是否需要将格式更改为日期格式以应用RegexFilter

I tried using the following, but failed : 我尝试使用以下方法,但失败了:

DefaultTableModel model = (DefaultTableModel) easypath.masteBusiness_table.getModel();
easypath.masteBusiness_table.setModel(model);
TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(easypath.masteBusiness_table.getModel());
easypath.masteBusiness_table.setRowSorter(rowSorter);
rowSorter.setRowFilter(RowFilter.regexFilter(startD+"\\s+(.*?)\\s+"+endD));

I know I am wrong with the filtering as (startD+"\\\\s+(.*?)\\\\s+"+endD)); 我知道我在(startD+"\\\\s+(.*?)\\\\s+"+endD));进行过滤是错误的(startD+"\\\\s+(.*?)\\\\s+"+endD)); is only for searching a single string but as a novice I would very much appreciate any suggestion. 仅用于搜索单个字符串,但是作为新手,我非常感谢任何建议。

UPDATE 更新
I just saw this ( http://docs.oracle.com/javase/7/docs/api/javax/swing/RowFilter.html ) where RowSorter<M,I> , M is the model and I is an integer value, which does not match my criteria 我刚刚看到了这个( http://docs.oracle.com/javase/7/docs/api/javax/swing/RowFilter.html ),其中RowSorter<M,I> ,M是模型,而I是整数,不符合我的标准

You should be storing a Date in the TableModel not a String representation of the date. 您应该在TableModel存储Date而不是Date的String表示形式。

Then you can use a RowFilter . 然后,您可以使用RowFilter

Instead of a regex filter you can use an "and" filter with two date filters. 可以使用带有两个日期过滤器的“和”过滤器来代替正则表达式过滤器。 Something like: 就像是:

List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add( RowFilter.dateFilter(ComparisonType.AFTER, startDate) );
filters.add( RowFilter.dateFilter(ComparisonType.BEFORE, endDate) );
rf = RowFilter.andFilter(filters);
sorter.setRowFilter(rf);

Read the Swing tutorial on Sorting and Filtering for more information and working examples on how to use a filter. 阅读有关排序和过滤的Swing教程,以获取更多信息以及有关如何使用过滤器的工作示例。

You should have an atributte JTable table and DefaultTableModel dtm . 您应该有一个属性JTable tableDefaultTableModel dtm Then in the function to filter by a range of dates. 然后在函数中按日期范围进行过滤。

public void filter(Date startDate, Date endDate){
    List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
    filters.add( RowFilter.dateFilter(ComparisonType.AFTER, startDate) );
    filters.add( RowFilter.dateFilter(ComparisonType.BEFORE, endDate) );
    dtm = (DefaultTableModel) table.getModel();
    TableRowSorter<DefaultTableModel> tr = new TableRowSorter<>(dtm);
    table.setRowSorter(tr);
    RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
    tr.setRowFilter(rf);}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM