views:

555

answers:

1

I have an editable JComboBox where I want to take some action whenever the text is changed, either by typing or selection. In this case, the text is a pattern and I want to verify that the pattern is valid and show the matches that result in some test data.

Having done the obvious, attach an ActionHandler, I have found that, for typing, the event seems to fire unreliably, at best (selection is fine). And when it does fire as a result of typing, the text retrieved (using getEditor().getItem(), since getSelectedItem() only gets the text when it was selected from the list) seems to be the text as it was when the last event was fired - that is, it's always missing the character was typed immediately before the action event was fired.

I was expecting the action event to fire after some short delay (500ms to 1 second), but it seems immediately fired upon keying (if it is fired at all).

The only workable alternative I can think of is to simply start a 1 second timer on focus-gained, killing it on focus-lost and doing the work as the timer action if the content is different from last time.

Any thoughts or suggestions?

The code snippets are not particularly interesting:

find.addActionListener(this);
...
public void actionPerformed(ActionEvent evt) {
    System.out.println("Find: "+find.getEditor().getItem());
    }
+6  A: 

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 ...
});
Dave Ray
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
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
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
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
I added another example using anonymous classes.
Dave Ray