I would like to add that the changedUpdate method will not trigger a notification for text documents. If you are using a text-based text component, you must use insertUpdate and / or removeUpdate.
Recently, I had to use a document listener as a way to disable / enable a button if the user edited the combo box. I did something like this and worked very well:
public class MyDocumentListener implements DocumentListener { @Override public void insertUpdate(DocumentEvent e) { setChanged(); notifyObservers(true); } @Override public void removeUpdate(DocumentEvent e) { setChanged(); notifyObservers(false); } @Override public void changedUpdate(DocumentEvent e) { // Not used when document is plain text } }
Then I added this listener to the combo box as follows:
((JTextComponent) combobox.getEditor().getEditorComponent()) .getDocument().addDocumentListener(new MyDocumentListener());
This works because the document associated with the combo box is plain text. When I used changeUpdate, I did not.
hfontanez
source share