我正在使用JComboBox
,如果所选项目被更改,则会触发另一帧的打开。
这是一个代码示例。
String[] liste = {"Stack", "Over", "Flow"};
JComboBox comboBox = new JComboBox(liste);
comboBox.addItemListener(e -> {
if(e.getStateChange() == 2) { //because that listener is called twice, when the item is unselected and the new is selected
FramePopUp popUp1 = new FramePopUp(); //Class that extends JFrame
}
});
我的问题是,当用户选择两次相同的项目时,我也想要触发此帧。
例如:
在这种情况下,用户选择项目“Over”,Frame Pop向上,用户处理Frame以及他必须给出的信息,然后关闭Frame。
他记得他忘了填写一个信息,所以他重新选择“Over”项目,但没有任何反应,因为他没有更改所选项目。
只需添加一个ActionListener而不是ItemListener。
另见JCombobox - Only execute actionlistener when value changes,其中有人遇到了与你相反的问题