简体   繁体   中英

How to filter rows in JTable based on boolean valued columns?

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.

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;
 }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM