How can I know when the text of an editable JComboBox has been changed?

The action listener is typically only fired when you hit enter, or move focus away from the editor of the combobox. The correct way to intercept individual changes to the editor is to register a document listener.

The action listener is typically only fired when you hit enter, or move focus away from the editor of the combobox. The correct way to intercept individual changes to the editor is to register a document listener: final JTextComponent tc = (JTextComponent) combo.getEditor(). GetEditorComponent(); tc.getDocument().

AddDocumentListener(this); The DocumentListener interface has methods that are called whenever the Document backing the editor is modified (insertUpdate, removeUpdate, changeUpdate). You can also use an anonymous class for finer-grained control of where events are coming from: final JTextComponent tcA = (JTextComponent) comboA.getEditor(). GetEditorComponent(); tcA.getDocument().

AddDocumentListener(new DocumentListener() { ... code that uses comboA ... }); final JTextComponent tcB = (JTextComponent) comboB.getEditor(). GetEditorComponent(); tcB.getDocument(). AddDocumentListener(new DocumentListener() { ... code that uses comboB ... }).

Thanks, I'll give this a try. I don't mind Swing as a whole, but sometimes working out how to get certain behavior or trap certain events is FAR from obvious. – Software Monkey Aug 10 '09 at 20:19 OK, so putting aside the "19" levels of method call... Having added a document listener, when the event occurs I can find no way to tie it to which of the two combo-boxes were changed.At this point, a simple timer's looking rather attractive.

– Software Monkey Aug 11 '09 at 5:27 I stored the last known text for the two CBs, and use that to determine which has changed when the document event is fired. If you know how to tie the event back to the component which fired it that would be useful info - since a DocumentEvent is not a standard AWT event or java.util.EventObject. – Software Monkey Aug 11 '09 at 5:55 The document doesn't really know about the component it's hooked to by design.

That way you can have multiple "views" of the same document. Rather than a single DocumentListener implementation, create a separate one for each combo box.An anonymous class would probably be good here. – Dave Ray Aug 11 '09 at 13:27 I added another example using anonymous classes.

– Dave Ray Aug 11 '09 at 13:30.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions