use*_*815 1 java swing jtable rowfilter
我无法完成这项工作,我发现的示例只能使用单个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
Run Code Online (Sandbox Code Playgroud)
如果有人可以为我在最后一行中尝试做的事情提供一个工作片段,那将非常感激.目前维护两个独立的表模型来避免这个问题,我想避免重复数据.
谢谢,凯
你的最后一行甚至没有编译,因为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);
Run Code Online (Sandbox Code Playgroud)
请确保使用适当的表模型初始化TableRowSorter.