Bernard
Bernard

Reputation: 4580

What's the appropriate listener for JComboBox for updating itself?

I have a JComboBox which it's items are updating with other parts of program every second. I need a listener when the user tried to type anything there or tried to select an item from JComboBox it updates it's content and show the new items added. I used actionPerformed but it is bringing new items but user can not select then I used itemStateChanged but the program crashed and I had to close netbeans!

Answer: My JcomboBox was on the JPanel. The best way I figure out was to add the listener on JPanel so any element on JPanel even JcomboBox get clicked it will update the comboBox.


My JcomboBox was on the JPanel. The best way I figure out was to add the listener on JPanel so any element on JPanel even JcomboBox get clicked it will update the comboBox.

Upvotes: 0

Views: 516

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347244

Events effecting the contents of the combo box are generated by the model.

Try attaching a ListDataListener to the model itself.

If you are worried about being notified when/if the model changes, you would need to attach a PropertyChangeListener to the JComboBox and monitor for the model property and update your data listeners accordingly...

Upvotes: 1

Josh M
Josh M

Reputation: 11947

ItemListener for when user selects a different item.

KeyListener for when the user is typing into the JComboBox.

Perhaps your program kept crashing because you weren't using a model for your JComboBox (I'm assuming you kept setting all of the items again)

Upvotes: 0

Related Questions