Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());
    }
like image 215
Lawrence Dol Avatar asked Aug 10 '09 00:08

Lawrence Dol


People also ask

Which of these is used to determine whether the JComboBox is editable?

SetEditable() - Determines whether the JComboBox field is editable.

Which event gets generated when you select an item from a JComboBox?

Which event gets generated when you select an item from a JComboBox? Combo boxes also generate item events, which are fired when any of the items' selection state changes.

Which is the description of a JComboBox?

JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list .

What is the difference between JComboBox and JList box give one method of each of them?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.


1 Answers

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 ...
});
like image 181
Dave Ray Avatar answered Oct 22 '22 03:10

Dave Ray