I have a 12 filters for 12 columns on a JTable. Each filter is a TextField of type JTextField. Every time a user enters the data in the text box filter then search happens.
Example: Suppose say, I have 3 columns and 3 text boxes above them as filters. Now any thing which is typed in any of the text boxes my table filtering has to happen. It is happening great.
╔═════════════╦═════════════╦═════════════╗
║ [TextField] ║ [TextField] ║ [TextField] ║
╠═════════════╬═════════════╬═════════════╣
║ ║ ║ ║
╠═════════════╬═════════════╬═════════════╣
... ... ...
╠═════════════╬═════════════╬═════════════╣
║ ║ ║ ║
╚═════════════╩═════════════╩═════════════╝
Text field listener for each of the text field looks like this,
textField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) { method(); }
public void removeUpdate(DocumentEvent e) { method(); }
public void insertUpdate(DocumentEvent e) { method(); }
};
My question is, I am repeating this code for all the 12 text fields, which I was not really happy. Is this the only way we have ? or can some one suggest me a better way ?
See this tutorial for an example of an inner class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With