JComboBox用于更新的适当监听器是什么?

时间:2021-10-27 11:31:24

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!

我有一个JComboBox,它的项目每秒都会与程序的其他部分一起更新。当用户尝试在那里输入任何内容或尝试从JComboBox中选择一个项目时,我需要一个监听器,它会更新它的内容并显示添加的新项目。我使用actionPerformed但它带来了新的项目,但是用户无法选择然后我使用了itemStateChanged但是程序崩溃了,我不得不关闭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.

答:我的JcomboBox在JPanel上。我弄清楚的最好的方法是在JPanel上添加监听器,这样JPanel上的任何元素甚至JcomboBox都会被点击它会更新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.

我的JcomboBox在JPanel上。我弄清楚的最好的方法是在JPanel上添加监听器,这样JPanel上的任何元素甚至JcomboBox都会被点击它会更新comboBox。

2 个解决方案

#1


1  

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

影响组合框内容的事件由模型生成。

Try attaching a ListDataListener to the model itself.

尝试将ListDataListener附加到模型本身。

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...

如果您担心在/如果模型更改时收到通知,则需要将PropertyChangeListener附加到JComboBox并监视模型属性并相应地更新数据侦听器...

#2


0  

ItemListener for when user selects a different item.

用户选择其他项目时的ItemListener。

KeyListener for when the user is typing into the JComboBox.

用户在JComboBox中键入时的KeyListener。

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)

也许你的程序一直在崩溃,因为你没有为你的JComboBox使用模型(我假设你再次设置了所有的项目)

#1


1  

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

影响组合框内容的事件由模型生成。

Try attaching a ListDataListener to the model itself.

尝试将ListDataListener附加到模型本身。

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...

如果您担心在/如果模型更改时收到通知,则需要将PropertyChangeListener附加到JComboBox并监视模型属性并相应地更新数据侦听器...

#2


0  

ItemListener for when user selects a different item.

用户选择其他项目时的ItemListener。

KeyListener for when the user is typing into the JComboBox.

用户在JComboBox中键入时的KeyListener。

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)

也许你的程序一直在崩溃,因为你没有为你的JComboBox使用模型(我假设你再次设置了所有的项目)