问题描述
每当从 JComboBox
中做出选择时,我都会尝试触发事件。
I'm trying to get an event to fire whenever a choice is made from a JComboBox
.
我遇到的问题是没有明显的 addSelectionListener()
方法。
The problem I'm having is that there is no obvious addSelectionListener()
method.
我试过使用 actionPerformed()
但它永远不会触发。
I've tried to use actionPerformed()
but it never fires.
没有覆盖的模型JComboBox
我没有想法。
如何在上收到有关选择更改的通知JComboBox
?
How do I get notified of a selection change on a JComboBox
?
编辑:我不得不道歉,事实证明我使用了一个行为不端的子类 JComboBox
,但我会保留问题,因为你的答案是好的。开始投票。 :)
I have to apologize it turns out I was using a misbehaving subclass of JComboBox
, but I'll leave the question up since your answer is good. Commence the vote down. :)
推荐答案
它应该响应,如下所示:
It should respond to ActionListeners, like this:
combo.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
doSomething();
}
});
正确地指出 addItemListener()
也会起作用。但是,您可以获得2 ItemEvents
,一个用于取消选择先前选择的项目,另一个用于选择新项目。只是不要使用这两种事件类型!
@John Calsbeek rightly points out that addItemListener()
will work, too. You may get 2 ItemEvents
, though, one for the deselection of the previously selected item, and another for the selection of the new item. Just don't use both event types!
这篇关于JComboBox选择更改侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!