我正在尝试在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*/
}
Run Code Online (Sandbox Code Playgroud)
您可以注意到,当我需要向组合添加元素时,另一个事件被触发并且再次调用actionPerformed方法,即使我不想要它,并且代码可能循环... :(有没有办法到只听选择更改而不是一般的更改事件?谢谢