“连接” RowFilter 的 andFilter 和 orFilter
我有一个包含四列的 JTable,第一列包含数字或文本,其他三列仅包含文本。我正在尝试在 RowFilter 的帮助下过滤此表:
sorter = new TableRowSorter<TableModel>(myOwnTableModel);
我得到的 checkboxFilter 工作得很好:
sorter.setRowFilter(RowFilter.regexFilter("^[0-9]$", 0));
此排序器根据设置或未设置的复选框来激活或停用。
如果用户在文本字段中放置一些文本,则会发生第二次过滤。就其本身而言,这已经很好了:
String regex = "(?i)" + Pattern.quote(s); // s = input Text of user
sorter.setRowFilter(RowFilter.regexFilter(regex, 1,2,3));
我不能做的是同时激活两个过滤器。也许我想得太远了,我的想法是“连接”两个过滤器,checkboxFilter应该是“和”另一个“或”。我尝试了几件事,对我来说最有希望的是:
String regex = "(?i)" + Pattern.quote(s);
bookFilter = RowFilter.regexFilter(regex, 1,2,3);
sorter.setRowFilter(bookFilter.andFilter(Arrays.asList(
RowFilter.regexFilter("^[0-9]$", 0))));
不幸的是,这不会产生任何可用的结果。任何想法表示赞赏:)
I have a JTable with four columns, the first one containing either a number or a text, the other three only text. I'm trying to filter this table with the help of a RowFilter:
sorter = new TableRowSorter<TableModel>(myOwnTableModel);
The checkboxFilter I got works well enough:
sorter.setRowFilter(RowFilter.regexFilter("^[0-9]$", 0));
This sorter is activated or deactivate depending on a checkbox that is either set or not.
The second filtering happens if a user puts some text in a textfield. For itself, this works fine already:
String regex = "(?i)" + Pattern.quote(s); // s = input Text of user
sorter.setRowFilter(RowFilter.regexFilter(regex, 1,2,3));
What I can't do, is to activate both filters at the same time. Maybe I'm thinking way too far, my idea has been to "concatenate" the two filters, the checkboxFilter should be "and" the other "or". I tried several things, to me the most promising looked something like:
String regex = "(?i)" + Pattern.quote(s);
bookFilter = RowFilter.regexFilter(regex, 1,2,3);
sorter.setRowFilter(bookFilter.andFilter(Arrays.asList(
RowFilter.regexFilter("^[0-9]$", 0))));
Unfortunately, this doesn't lead to any usable result. Any ideas appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是向
JCheckBox
添加一个ActionListener
,以便在切换复选框时更新筛选器状态,并向JCheckBox 添加一个
的基础文档,用于在字段内容更新时更新过滤器状态。DocumentListener
>JTextField代码中的另一个错误是您在
bookFilter
实例上调用静态andFilter
方法,并且仅传入新构造的正则表达式过滤器(即您是只向andFilter
传递一个参数)。正确的用法是:示例事件侦听器
...,然后定义 updateFilters() 方法,根据复选框被选中的时间以及文本字段是否为空来安装新的过滤器。
过滤器更新方法示例
The solution is to add an
ActionListener
to theJCheckBox
to update the filter state if the checkbox is toggled and to add aDocumentListener
to theJTextField
's underlying Document to update the filter state if the contents of the field is updated.The other bug in your code is that you are calling the static
andFilter
method on yourbookFilter
instance and are only passing in the newly constructed regex filter (i.e. you are only passing in one parameter toandFilter
). The correct usage is:Example Event Listeners
... and then define your updateFilters() method to install a new filter based on when the checkbox is selected and whether the text field is empty or not.
Example Filter Update Method