简体   繁体   English

JTable使用RowFilter比较两列

[英]JTable use RowFilter to compare two columns

I have a JTable with a certain number of columns (all String). 我有一个具有一定列数(全部为String)的JTable I would like to implement a filter that shows only the rows where the column2 and the column3 containing the same string. 我想实现一个过滤器,该过滤器仅显示column2和column3包含相同字符串的行。 Is it possible? 可能吗? I tried with the regExp but I don't see the way to compare table cell values. 我尝试使用regExp,但看不到比较表单元格值的方法。

simply implement RowFilter directly: 只需直接实现RowFilter即可:

  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. 您可以使用RowFilter来比较两列中的数据。

http://download.oracle.com/javase/6/docs/api/javax/swing/RowFilter.html 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;
        }
   }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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