RowFilter.regex过滤多列

发布于 2024-08-26 07:13:26 字数 718 浏览 7 评论 0原文

我目前正在使用以下内容来过滤我的 JTable

RowFilter.regexFilter( 
         Pattern.compile(textField.getText(), 
         Pattern.CASE_INSENSITIVE).toString(),     columns );

如何格式化我的 textField 或过滤器,以便如果我想过滤多个列,我可以这样做。现在我可以过滤多列,但我的过滤器只能是其中一列的

一个示例可能有助于我更好地解释:

Name Grade GPA
Zac   A    4.0
Zac   F    1.0
Mike  A    4.0
Dan   C    2.0

文本字段将包含 Zac A 或类似的内容,并且它将显示第一个 Zac如果 columnsint[]{0, 1},则为 row。现在,如果我执行上述操作,我什么也得不到。过滤器 Zac 有效,但我得到了两个 Zac 的过滤器。 A 也可以,但我会得到 Zac A 4.0Mike A 3.0

我希望我已经很好地解释了我的问题。如果您不明白,请告诉我。

I am currently using the following to filter my JTable

RowFilter.regexFilter( 
         Pattern.compile(textField.getText(), 
         Pattern.CASE_INSENSITIVE).toString(),     columns );

How do I format my textField or filter so if I want to filter multiple columns I can do that. Right now I can filter multiple columns but my filter can only be of one of the columns

An example might help my explanation better:

Name Grade GPA
Zac   A    4.0
Zac   F    1.0
Mike  A    4.0
Dan   C    2.0

The text field would contain Zac A or something similar and it would show the first Zac row if columns was int[]{0, 1}. Right now if I do the above I get nothing. The filter Zac works but I get both Zac's. A also works but I would then get Zac A 4.0 and Mike A 3.0.

I hope I have explained my problem well. Please let me know if you do not understand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

在梵高的星空下 2024-09-02 07:13:26

看起来您需要为每一列创建一个单独的过滤器,并将它们与 AndFilter,或者通过重写 include() 方法。 RegexFilter 仅需要指定的列之一进行匹配,并且似乎没有办法更改它。

顺便说一句,如果你想强制正则表达式忽略大小写,你应该在它的开头添加 (?i) 。尽管您使用了 CASE_INSENSITIVE 标志,但您生成的字符串与开始时的字符串相同。

编辑:我链接到的文档包含一个从两个 RegexFilter 创建 AndFilter 的示例,但该示例非常愚蠢。它创建一个过滤器,在任何列中查找 foo 或在任何列中查找 bar - 这与带有 foo|bar 的单个 RegexFilter 完全相同code> 作为正则表达式并且没有指定列。一个好的 AndFilter 示例应该执行只有 AndFilter 才能执行的操作:同时对两个或多个列强制执行条件,就像您尝试做的那样。以下是我如何不区分大小写地过滤第一列中的 Zac 和第二列中的 A

List<RowFilter<Object,Object>> rfs = 
    new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter("(?i)^Zac$", 0));
filters.add(RowFilter.regexFilter("(?i)^A$", 1));
RowFilter<Object,Object> af = RowFilter.andFilter(rfs);

另外,如果我接受过滤器文本作为用户输入(您似乎认为要做),我可能会首先引用它:

String regex = "(?i)^" + Pattern.quote(input) + "$";

Looks like you either need to create a separate filter for each column and combine them with an AndFilter, or write your own filter by overriding the include() method. A RegexFilter only requires one of the specified columns to match, and there doesn't seem to be a way to change that.

By the way, if you want to force the regex to ignore case, you should add (?i) to the beginning of it. The string you're generating is the same as the one you started with, despite your use of the CASE_INSENSITIVE flag.

EDIT: The doc I linked to contains an example of creating an AndFilter from two RegexFilters, but the example is pretty silly. It creates a filter that looks for foo in any column or bar in any column--which is exactly the same as a single RegexFilter with foo|bar as the regex and no columns specified. A good AndFilter example should do something only an AndFilter can do: enforce conditions on two or more columns at once, like you're trying to do. Here's how I would filter case-insensitively for Zac in the first column and A in the second:

List<RowFilter<Object,Object>> rfs = 
    new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter("(?i)^Zac$", 0));
filters.add(RowFilter.regexFilter("(?i)^A$", 1));
RowFilter<Object,Object> af = RowFilter.andFilter(rfs);

Also, if I were accepting the filter text as user input (which you seem to be doing), I would probably quote it first:

String regex = "(?i)^" + Pattern.quote(input) + "$";
断肠人 2024-09-02 07:13:26

通过结合oracle关于表的教程Alan 的答案,我做了这个:

RowFilter<PublicationTableModel, Object> rf = null;
List<RowFilter<Object,Object>> rfs = 
            new ArrayList<RowFilter<Object,Object>>();

try {
    String text = txtFilter.getText();
    String[] textArray = text.split(" ");

    for (int i = 0; i < textArray.length; i++) {
        rfs.add(RowFilter.regexFilter("(?i)" + textArray[i], 0, 1, 2, 4));
    }

    rf = RowFilter.andFilter(rfs);

} catch (java.util.regex.PatternSyntaxException e) {
        return;
}

sorter.setRowFilter(rf);

这是一个非常简单的过滤器,它从文本框,分割单词并动态创建许多过滤器。然后,将它们与 andFilter 组合并放回到表模型的排序器中。

By combining the oracle's tutorial on tables and the answer from Alan, I made this:

RowFilter<PublicationTableModel, Object> rf = null;
List<RowFilter<Object,Object>> rfs = 
            new ArrayList<RowFilter<Object,Object>>();

try {
    String text = txtFilter.getText();
    String[] textArray = text.split(" ");

    for (int i = 0; i < textArray.length; i++) {
        rfs.add(RowFilter.regexFilter("(?i)" + textArray[i], 0, 1, 2, 4));
    }

    rf = RowFilter.andFilter(rfs);

} catch (java.util.regex.PatternSyntaxException e) {
        return;
}

sorter.setRowFilter(rf);

It's a pretty simple filter that takes all the text from a textbox, splits the words and dynamically creates many filters. Then, combining them with an andFilter and putting in back to the sorter for the table model.

雨夜星沙 2024-09-02 07:13:26
RowFilter<TableModel, Object> filter =
    RowFilter.orFilter(Arrays.asList(RowFilter.regexFilter(lookup,0),
    RowFilter.regexFilter(lookup, 1)));

或者

RowFilter<TableModel, Object> filter =
    RowFilter.regexFilter(Pattern.compile(lookup,Pattern.CASE_INSENSITIVE).toString(),0,1);

0 和 1 是列号

RowFilter<TableModel, Object> filter =
    RowFilter.orFilter(Arrays.asList(RowFilter.regexFilter(lookup,0),
    RowFilter.regexFilter(lookup, 1)));

or

RowFilter<TableModel, Object> filter =
    RowFilter.regexFilter(Pattern.compile(lookup,Pattern.CASE_INSENSITIVE).toString(),0,1);

0 and 1 are the column numbers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文