[英]JTable use RowFilter to compare two columns
我有一个具有一定列数(全部为String)的JTable
。 我想实现一个过滤器,该过滤器仅显示column2和column3包含相同字符串的行。 可能吗? 我尝试使用regExp,但看不到比较表单元格值的方法。
只需直接实现RowFilter即可:
RowFilter filter = new RowFilter<Object, Integer>() {
@Override
public boolean include(Entry entry) {
return entry.getValue(firstColumn).equals(entry.getValue(secondColumn));
}
}
(省略空检查和完整的泛型)
您可以使用RowFilter来比较两列中的数据。
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.