问题描述
如何在两个日期之间实现RowFilter
?日期为字符串格式.是否有必要将格式更改为日期格式以应用RegexFilter
?
How can I implement a RowFilter
between two dates? The dates are in string format. Is it necessary to change the format to a date format to apply a 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));
我知道我对过滤错误,因为(startD+"\\s+(.*?)\\s+"+endD));
仅用于搜索单个字符串,但作为新手,我非常感谢任何建议.
I know I am wrong with the filtering as (startD+"\\s+(.*?)\\s+"+endD));
is only for searching a single string but as a novice I would very much appreciate any suggestion.
更新
我刚刚看到了这个内容( http://docs.oracle. com/javase/7/docs/api/javax/swing/RowFilter.html ),其中RowSorter<M,I>
,M是模型,而I是不符合我的条件的整数值
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
推荐答案
您应该将Date
存储在TableModel
中,而不是日期的字符串表示形式.
You should be storing a Date
in the TableModel
not a String representation of the date.
然后您可以使用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);
在排序和过滤上阅读Swing教程.有关如何使用过滤器的更多信息和工作示例.
Read the Swing tutorial on Sorting and Filtering for more information and working examples on how to use a filter.
这篇关于JTable在两个日期之间的rowfilter,同一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!