“连接” RowFilter 的 andFilter 和 orFilter

发布于 2024-10-03 13:19:57 字数 876 浏览 0 评论 0原文

我有一个包含四列的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-10-10 13:19:57

解决方案是向 JCheckBox 添加一个 ActionListener,以便在切换复选框时更新筛选器状态,并向 JCheckBox 添加一个 DocumentListener >JTextField 的基础文档,用于在字段内容更新时更新过滤器状态。

代码中的另一个错误是您在 bookFilter 实例上调用静态 andFilter 方法,并且仅传入新构造的正则表达式过滤器(即您是只向 andFilter 传递一个参数)。正确的用法是:

RowFilter andFilter = RowFilter.andFilter(filter1, filter2, etc);

示例事件侦听器

JCheckBox cb = ...
cb.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    updateFilters();
  }
});

JTextField tf = ...
tf.getDocument().addDocumentListener(new DocumentListener() {
  public void insertUpdate(DocumentEvent e) { updateFilters(); }
  public void removeUpdate(DocumentEvent e) { updateFilters(); }
  publci void changedUpdate(DocumentEvent e) { updateFilters(); }
});

...,然后定义 updateFilters() 方法,根据复选框被选中的时间以及文本字段是否为空来安装新的过滤器。

过滤器更新方法示例

public void updateFilters() {
  if (cb.isSelected()) {
    if (tf.getText().length() > 0) {
       // Both filters active so construct an and filter.
       sorter.setRowFilter(RowFilter.andFilter(bookFilter, checkBoxFilter));
    } else {
       // Checkbox selected but text field empty.
       sorter.setRowFilter(checkBoxFilter);
    }
  } else if (tf.getText().length() > 0) {
    // Checkbox deselected but text field non-empty.
    sorter.setRowFilter(bookFilter);
  } else {
    // Neither filter "active" so remove filter from sorter.
    sorter.setRowFilter(null); // Will cause table to re-filter.
  }
}

The solution is to add an ActionListener to the JCheckBox to update the filter state if the checkbox is toggled and to add a DocumentListener to the JTextField'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 your bookFilter instance and are only passing in the newly constructed regex filter (i.e. you are only passing in one parameter to andFilter). The correct usage is:

RowFilter andFilter = RowFilter.andFilter(filter1, filter2, etc);

Example Event Listeners

JCheckBox cb = ...
cb.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    updateFilters();
  }
});

JTextField tf = ...
tf.getDocument().addDocumentListener(new DocumentListener() {
  public void insertUpdate(DocumentEvent e) { updateFilters(); }
  public void removeUpdate(DocumentEvent e) { updateFilters(); }
  publci void changedUpdate(DocumentEvent e) { updateFilters(); }
});

... 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

public void updateFilters() {
  if (cb.isSelected()) {
    if (tf.getText().length() > 0) {
       // Both filters active so construct an and filter.
       sorter.setRowFilter(RowFilter.andFilter(bookFilter, checkBoxFilter));
    } else {
       // Checkbox selected but text field empty.
       sorter.setRowFilter(checkBoxFilter);
    }
  } else if (tf.getText().length() > 0) {
    // Checkbox deselected but text field non-empty.
    sorter.setRowFilter(bookFilter);
  } else {
    // Neither filter "active" so remove filter from sorter.
    sorter.setRowFilter(null); // Will cause table to re-filter.
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文