简体   繁体   中英

JTable use RowFilter to compare two columns

I have a JTable with a certain number of columns (all String). I would like to implement a filter that shows only the rows where the column2 and the column3 containing the same string. Is it possible? I tried with the regExp but I don't see the way to compare table cell values.

simply implement RowFilter directly:

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

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

(null checks and full generics omitted)

You can use a RowFilter that compares the data in both columns to each other.

http://download.oracle.com/javase/6/docs/api/javax/swing/RowFilter.html

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

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