Java JComboBox 选择更改侦听器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JComboBox Selection Change Listener?
提问by Allain Lalonde
I'm trying to get an event to fire whenever a choice is made from a JComboBox
.
每当从JComboBox
.
The problem I'm having is that there is no obvious addSelectionListener()
method.
我遇到的问题是没有明显的addSelectionListener()
方法。
I've tried to use actionPerformed()
, but it never fires.
我尝试使用actionPerformed()
,但它永远不会触发。
Short of overriding the model for the JComboBox
, I'm out of ideas.
缺少覆盖模型的JComboBox
,我没有想法。
How do I get notified of a selection change on a JComboBox
?**
我如何在JComboBox
? **上收到选择更改的通知?
Edit: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.
编辑:我必须道歉。事实证明,我使用了一个行为不端的子类JComboBox
,但由于您的回答很好,所以我将这个问题搁置一旁。
采纳答案by jodonnell
It should respond to ActionListeners, like this:
它应该响应ActionListeners,如下所示:
combo.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
doSomething();
}
});
@John Calsbeekrightly 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!
@John Calsbeek正确地指出这addItemListener()
也行得通。ItemEvents
但是,您可能会得到 2 ,一个用于取消选择先前选择的项目,另一个用于选择新项目。只是不要同时使用这两种事件类型!
回答by John Calsbeek
I would try the itemStateChanged()
method of the ItemListener
interface if jodonnell's solution fails.
我会尝试itemStateChanged()
的方法ItemListener
如果jodonnell的解决方案失败接口。
回答by JavaKeith
You may try these
你可以试试这些
int selectedIndex = myComboBox.getSelectedIndex();
-or-
-或者-
Object selectedObject = myComboBox.getSelectedItem();
-or-
-或者-
String selectedValue = myComboBox.getSelectedValue().toString();
回答by Viacheslav
Code example of ItemListener
implementation
ItemListener
实现代码示例
class ItemChangeListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
Object item = event.getItem();
// do something with object
}
}
}
Now we will get only selected item.
现在我们将只获得选定的项目。
Then just add listener to your JComboBox
然后只需将侦听器添加到您的 JComboBox
addItemListener(new ItemChangeListener());
回答by Craig Wayne
I was recently looking for this very same solution and managed to find a simple one without assigning specific variables for the last selected item and the new selected item. And this question, although very helpful, didn't provide the solution I needed. This solved my problem, I hope it solves yours and others. Thanks.
我最近正在寻找这个非常相同的解决方案,并设法找到了一个简单的解决方案,而无需为最后一个选定项目和新选定项目分配特定变量。这个问题虽然很有帮助,但并没有提供我需要的解决方案。这解决了我的问题,我希望它能解决你和其他人的问题。谢谢。
回答by Ahuramazda
Here is creating a ComboBox adding a listener for item selection change:
这是创建一个 ComboBox,为项目选择更改添加一个侦听器:
JComboBox comboBox = new JComboBox();
comboBox.setBounds(84, 45, 150, 20);
contentPane.add(comboBox);
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setBounds(84, 97, 150, 20);
contentPane.add(comboBox_1);
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
//Do Something
}
});