I have the following code:
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);
} }
);
}
Currently the document listener is set to the textfield filterR but i want the listener to also include filterR1 and filterR2 as i have a search filter further down in the code that searches textfield filterR1 and filterR2 ie a combined filter search.
How do i change my document listener to cover more than just 1 textfield?
Well, you could assign the DocumentListener
to a variable
DocumentListener docListener = new DocumentListener() {
...
};
And the use it for all your textfields
filterR.getDocument().addDocumentListener(docListener);
filterR1.getDocument().addDocumentListener(docListener);
filterR2.getDocument().addDocumentListener(docListener);
...