2011-04-12 41 views
1

我有一個JTable與一定數量的列(所有字符串)。我想實現一個過濾器,只顯示包含相同字符串的column2和column3的行。 這可能嗎?我嘗試了regExp,但我沒有看到比較表格單元值的方式。JTable使用RowFilter來比較兩列

回答

3

直接實現的RowFilter:

RowFilter filter = new RowFilter<Object, Integer>() { 

     @Override 
     public boolean include(Entry entry) { 
      return entry.getValue(firstColumn).equals(entry.getValue(secondColumn)); 
     } 
    } 

(null檢查和充分的仿製藥省略)

0
 //col as int is in outer class. Pls note col is your column to specify. 
    //So if you want more columns use || and with another column say col2. 
    class Columnfilter extends RowFilter<Object, Integer> 
    { String txt=null; 
    Columnfilter(String txt){ 
      this.txt=txt; 
     } 

     void setText(String txt){ 
      this.txt=txt; 
     } 

    @Override 
    public boolean include(Entry entry) { 

     if(txt==null)return false; 
     if(col==-1)return true;//for all 
    String colVal= entry.getStringValue(col);//chk column if value exist 
     int b= colVal.toLowerCase().indexOf(txt.toLowerCase()); 

     if(b!=-1){ 
      return true; 
     }else{ 
      return false; 
     } 
    }}