Java Swing:将RowFilter.andFilter与RowFilter.orFilter结合使用
问题描述:
我无法完全实现这个功能,而我发现的例子只能使用一个RowFilter.andFilter或RowFilter.orFilter。有没有办法把两个结合得到像(A || B)& &(C || D)?以下是我正在尝试的一些示例代码。Java Swing:将RowFilter.andFilter与RowFilter.orFilter结合使用
ArrayList<RowFilter<Object,Object>> arrLstColorFilters = new ArrayList<RowFilter<Object,Object>>();
ArrayList<RowFilter<Object,Object>> arrLstCandyFilters = new ArrayList<RowFilter<Object,Object>>();
RowFilter<Object,Object> colorFilter;
RowFilter<Object,Object> candyFilter;
TableRowSorter<TableModel> sorter;
// OR colors
RowFilter<Object,Object> blueFilter = RowFilter.regexFilter("Blue", myTable.getColumnModel().getColumnIndex("Color"));
RowFilter<Object,Object> redFilter = RowFilter.regexFilter("Red", myTable.getColumnModel().getColumnIndex("Color"));
arrLstColorFilters.add(redFilter);
arrLstColorFilters.add(blueFilter);
colorFilter = RowFilter.orFilter(arrLstColorFilters);
// OR candies
RowFilter<Object,Object> mAndMFilter = RowFilter.regexFilter("M&M", myTable.getColumnModel().getColumnIndex("Candy"));
RowFilter<Object,Object> mentosFilter = RowFilter.regexFilter("Mentos", myTable.getColumnModel().getColumnIndex("Candy"));
arrLstCandyFilters.add(mAndMFilter);
arrLstCandyFilters.add(mentosFilter);
candyFilter = RowFilter.orFilter(arrLstCandyFilters);
// Mentos and M&Ms that are red or blue (this is where I'm stuck)
sorter.setRowFilter(RowFilter.andFilter(candyFilter, colorFilter); //this does not work
如果有人可以提供工作片段,我想在最后一行做什么,它将不胜感激。目前维护两个单独的表模型来规避这个问题,并且我想避免重复数据。
感谢, 凯
答
你的最后一行甚至不进行编译,因为andFilter
还需要一个列表,而不是单独的参数。
否则你的例子似乎在我的测试中发现。我换成你的例子与下面的代码的最后一行:
ArrayList<RowFilter<Object, Object>> andFilters = new ArrayList<RowFilter<Object, Object>>();
andFilters.add(candyFilter);
andFilters.add(colorFilter);
sorter = new TableRowSorter<TableModel>(myTable.getModel());
// Mentos and M&Ms that are red or blue
sorter.setRowFilter(RowFilter.andFilter(andFilters));
myTable.setRowSorter(sorter);
请确保您初始化相应的表模型TableRowSorter还。
+0
是的,你是对的,另一个中间数组列表。似乎需要更详细的一点,但是,是的,这就是API所说的......谢谢你的帮助。 – user644815 2011-03-04 19:05:41
也许如果你发布了一个带有真实数据的“unworking snippet”,那么有人可以创建一个“工作片段”。我们不知道你的真实数据是什么样子,所以很难创建和测试任何代码。 – camickr 2011-03-04 16:03:44
我想这更像是一个语法问题。您可以在api文档中分别创建RowFilter.orFilter和RowFilter.andFilter: http://download.oracle.com/javase/6/docs/api/javax/swing/RowFilter.html#andFilter%28java.lang.Iterable %29 http://download.oracle.com/javase/6/docs/api/javax/swing/RowFilter.html#orFilter%28java.lang.Iterable%29 – user644815 2011-03-04 17:15:49