Reputation: 4580
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
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
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