简体   繁体   中英

JComboBox, trigger an action if the selected item is change or the same is selected

I'm using a JComboBox that triggers the opening of another frame if the item selected is changed.

Here is an example of code.

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
    }
});

My issue is that I want this Frame to be triggered too when the user select twice the same item.

For example:

The user selects in this case the item "Over", the Frame Pop's up, the user deal with the Frame and the informations he have to give and then close the Frame.

He remembered that he forgot to fill one information, so he re-select the item "Over" but nothing happens because he did not change the selected item.

Just add an ActionListener instead of an ItemListener.

see also JCombobox - Only execute actionlistener when value changes where someone had the opposite problem from you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM