【发布时间】:2017-07-25 10:08:19
【问题描述】:
我正在尝试聆听 Java JComboBox 中的选择变化。我曾尝试使用 ActionListener,但问题是:动作侦听器会执行类似的操作
public void actionPerformed(ActionEvent e){
JComboBox<String> source = ((JComboBox<String>)e.getSource());
String selected = source.getItemAt(source.getSelectedIndex());
/*now I compare if the selected string is equal to some others
and in a couple of cases I have to add elements to the combo*/
}
如您所见,当我需要向组合中添加元素时,会触发另一个事件并再次调用 actionPerformed 方法,即使我不希望这样做,代码也可能会循环... :( 有没有办法只听选择更改而不是通用更改事件? 谢谢
【问题讨论】: