我有以下代码:
private void filter2method() {
filterR.getDocument().addDocumentListener(new DocumentListener() {//currently the document listener is set for textfield called filterR1
@Override
public void insertUpdate(DocumentEvent e) {
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e) {
newFilter();
}
@Override
public void changedUpdate(DocumentEvent e) {
newFilter();
}
private void newFilter() {
RowFilter <DefaultTableModel, Object>rf =null;
List<RowFilter<Object,Object>> rfs =
new ArrayList<RowFilter<Object,Object>>();
ArrayList textAL = new ArrayList();
String text = filterR.getText();
textAL.add(text);
String text1 = filterR1.getText();
textAL.add(text1);
String text2 = filterR2.getText();
textAL.add(text2);
try {
for (int i = 0; i < textAL.size(); i++) {
rfs.add(RowFilter.regexFilter("(?i)" + textAL.get(i), 2, 6, 11));
}
rf = RowFilter.andFilter(rfs);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
} }
);
}
目前文档侦听器设置为textfield filterR但我希望侦听器还包括filterR1和filterR2,因为我在搜索textfield filterR1和filterR2的代码中有一个搜索过滤器,即组合过滤器搜索。
如何更改文档侦听器以覆盖超过1个文本字段?
答案 0 :(得分:2)
好吧,您可以将DocumentListener
分配给变量
DocumentListener docListener = new DocumentListener() {
...
};
并将其用于所有文本字段
filterR.getDocument().addDocumentListener(docListener);
filterR1.getDocument().addDocumentListener(docListener);
filterR2.getDocument().addDocumentListener(docListener);
...