我不能很好地让它工作,而且我发现的示例只适用于单个RowFilter.andFilter或RowFilter.orFilter。有没有办法将两者结合起来,得到类似(A || B) && (C || D)的东西?下面是我正在尝试的一些示例代码。
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如果有人能为我在最后一行要做的事情提供一个工作代码片段,我将不胜感激。目前维护两个单独的表模型来避免这个问题,我希望避免重复数据。
谢谢你,凯
发布于 2011-03-05 01:29:31
最后一行甚至不能编译,因为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。
https://stackoverflow.com/questions/5194948
复制相似问题