filters = new ArrayList>(8); filters.add(Row-6ren">
gpt4 book ai didi

java - 我想在 Java GUI 中的表过滤器之间创建 "or"关系而不是 "and"

转载 作者:行者123 更新时间:2023-12-01 10:53:18 24 4
gpt4 key购买 nike

我想在这些过滤器之间创建“或”关系,而不是“与”关系,这就是它的作用。有什么办法可以做到这一点吗?

List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(8);
filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 1));
filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 2));
filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 3));
filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 4));

新代码

尝试{

         List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(8);

List<RowFilter<Object,Object>> filter = new ArrayList<RowFilter<Object,Object>>(4);
filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 1));
filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 2));
filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 3));
filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 4));

RowFilter<Object,Object> orFilter = RowFilter.orFilter(filter);

filters.add(orFilter);

String gen = (String) searchGender.getSelectedItem();
if(gen.equals("Male"))
gen = "M";
else
gen = "F";

filters.add(RowFilter.regexFilter(gen, 5));
filters.add(RowFilter.regexFilter(searchMailTxt.getText(), 6));
filters.add(RowFilter.regexFilter(searchContactTxt.getText(), 7));
filters.add(RowFilter.regexFilter(searchContactTxt.getText(), 8));

rf = RowFilter.andFilter(filters);

}catch(java.util.regex.PatternSyntaxException e){
System.out.println("Unable to parse");
return ;
}
sorter.setRowFilter(rf);

最佳答案

您可以使用orFilter方法:

filters.add(RowFilter.orFilter(Arrays.asList(new RowFilter[]{
RowFilter.regexFilter(searchNameTxt.getText(), 1),
RowFilter.regexFilter(searchNameTxt.getText(), 2),
// .....
})
));

但似乎regexFilter已经可以容纳多列(我认为只有其中一列需要匹配,所以这里应该有相同的结果):

filters.add(RowFilter.regexFilter(searchNameTxt.getText(), 1,2,3,4));

关于java - 我想在 Java GUI 中的表过滤器之间创建 "or"关系而不是 "and",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33732604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com