Java RowFilter.regexFilter 多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2494868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RowFilter.regexFilter multiple columns
提问by twodayslate
I am currently using the following to filter my JTable
我目前正在使用以下内容来过滤我的 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
我如何格式化我的textField
或过滤器,所以如果我想过滤多个列,我可以这样做。现在我可以过滤多个列,但我的过滤器只能是其中一列
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
.
文本字段将包含Zac A
或类似的内容,如果columns
是,它将显示第一个 Zac 行int[]{0, 1}
。现在,如果我执行上述操作,我将一无所获。过滤器Zac
有效,但我得到了两者Zac
。A
也有效,但我会得到Zac A 4.0
和Mike A 3.0
。
I hope I have explained my problem well. Please let me know if you do not understand.
我希望我已经很好地解释了我的问题。如果您不明白,请告诉我。
采纳答案by Alan Moore
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.
看起来您需要为每列创建一个单独的过滤器并将它们与AndFilter 组合,或者通过覆盖include()方法编写自己的过滤器。RegexFilter 只需要匹配指定的列之一,似乎没有办法改变它。
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.
顺便说一句,如果你想强制正则表达式忽略大小写,你应该添加(?i)
到它的开头。尽管您使用了 CASE_INSENSITIVE 标志,但您生成的字符串与您开始使用的字符串相同。
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:
编辑:我链接到的文档包含一个从两个 RegexFilters 创建 AndFilter 的示例,但该示例非常愚蠢。它创建一个foo
在任何列或bar
任何列中查找的过滤器——这与单个 RegexFilter 完全相同,带有foo|bar
正则表达式并且没有指定列。一个好的 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);
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) + "$";
回答by Scooby Doo
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
0 和 1 是列号
回答by Dimitrios K.
By combining the oracle's tutorial on tablesand the answer from Alan, I made this:
通过结合 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);
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.
这是一个非常简单的过滤器,它从文本框中获取所有文本,拆分单词并动态创建许多过滤器。然后,将它们与一个 andFilter 组合起来,并放回到表模型的排序器中。