views:

278

answers:

1

Im trying to filter rows based on a column say c1 that contains boolean values. I want to show only rows that have 'true' in c1. I looked up the examples in http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting. The example uses a regex filter. Is there any way I can use boolean values to filter rows?

Following is the code Im using (borrowed from the example)

private void filter(boolean show) {
  RowFilter<TableModel, Object> filter = null;
  TableModel model = jTb.getModel();
  boolean value = (Boolean) model.getValueAt(0,1);

    //If current expression doesn't parse, don't update.
    try {
         // I need to used  'value' to filter instead of filterText.
        filter =RowFilter.regexFilter(filterText, 0);
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(filter);

}

thank you.

A: 

Have you tried this example from the javadoc? (Slightly modified).

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.getValue(i).equals(true)) {
       return true;
      }
    }
    return false;
 }
};
Frederik Wordenskjold
Thanks for the example. Works exactly the way I want it to.
vinny
I was wrongly thinking Entry to refer to a cell entry. When I realised that Entry refers to an entire row, things became clear!
vinny
Glad it worked!
Frederik Wordenskjold