Search code examples
javajtablerowfilter

JTable row sorter - filter manage


I have a row filter but I need to load everything but a value...

Example:

col1 | col2
-----------
1    | N
2    | Y
3    | O

What I need:

col1 | col2
-----------
1    | N
3    | O

What I have:

RowFilter<TableModel, Object> firstFiler = null;
List<RowFilter<TableModel, Object>> filters = new 
ArrayList<RowFilter<TableModel, Object>>();
RowFilter<TableModel, Object> compoundRowFilter = null;
try {
    firstFiler = RowFilter.regexFilter("(?i)" + text, 1);
    filters.add(firstFiler);
    compoundRowFilter = RowFilter.andFilter(filters);
} catch (java.util.regex.PatternSyntaxException e) {
     return;
}
sorter.setRowFilter(compoundRowFilter);

Solution

  • See the example in the JavaDoc:

    https://docs.oracle.com/javase/8/docs/api/javax/swing/RowFilter.html

    The following example from the JavaDoc link above shows an include method that allows only entries containing one or more values starting with the string "a":

     RowFilter<Object,Object> startsWithAFilter = new RowFilter<Object,Object>() {
       public boolean include(Entry<? extends Object, ? extends Object> entry) {
         for (int i = entry.getValueCount() - 1; i >= 0; i--){            
           if(entry.getStringValue(i).startsWith("a")) {
             // The value starts with "a", include it
             return true;
           }
         }
         // None of the columns start with "a"; return false so that this
         // entry is not shown
         return false;
       }
     };
    

    You could modify the above example to instead include everything except "2".

    I recommend reading through the summary of that JavaDoc link to get a better understanding of how it works.