开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜