you are calling RowFilter.regexFilter(filterTF.getText(), 0);
in UserPage_Admin()
constructor. How it supposed to read the text from the filterTF. I think you should call it from an Action Event Listener assigned to a JButton
which will be called upon submitting(clicking) the text as follows:
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = filterText.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(text, 0));
}
}
});
If you want to use Filtering upon user key type event, add key listener to the text field you are taking input filter-string.
filterTxtFeild.addKeyListener(new KeyAdapter() {
public void keykeyReleased(KeyEvent evt) {
// on each key type event filter.
// put your filter code as submit button
}
});
However, as it is suggested in the comments below, to work with Swing Text Component, one should have used the Document.addDocumentListener(DocumentListener)
. A Swing text component uses a Document to represent its content. Document events occur when the content of a document changes in any way. Add the document listener as follows:
filterTxtFeild.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
// put your filter code here upon data insertion
}
@Override
public void removeUpdate(DocumentEvent e)
{
//put your filter code here upon data removal
}
@Override
public void changedUpdate(DocumentEvent e) {}
});
Edit: Why DocumentListener is preferable ?
If we want validation of input in the data source, using KeyEvent
while filtering the data you’ll find it does not reflect the user’s keystroke and input events are sent to the listeners before they are processed by the data source. suppose that when we want an user name
to be entered, someone input a text like "$%^&"
. On such invalid input, KeyEvent
will still be fired even though no valid changes has been made to data source. But, DocumentListeners
are notified only when a valid changes has been made to the data source. Data entry components produce events in which a validator
can listen for asynchronously
, one should never modify the contents of a text component from within a DocumentListener
. If we do so, the program will likely deadlock.