Question

I am trying to add a condition inside my ItemListener and only if the condition is verified the item gets selected, otherwise I dont want the user to be able to select that item.

How can I invalidade a selection on item listener? Make a new selection inside the item listener? won't that be a infinite loop? :o

Thanks alot in advance. Here is my code:

private final class classeComboBoxItemListener implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {


            try {
                if(e.getStateChange() == ItemEvent.DESELECTED)
                    updateLabelLugares(true, (Classe) e.getItem());

                if(e.getStateChange() == ItemEvent.SELECTED)
                    updateLabelLugares(false, (Classe) e.getItem());
            } catch (Exception e1) {


                /// HERE I WANT TO INVALIDATE THE SELECTION
                /// Returning to the item selected before!

            }

        }
    }
Était-ce utile?

La solution

How can I invalidade a selection on item listener? Make a new selection inside the item listener? won't that be a infinite loop? :o

  • remove ItemListener from JComboBox, then call JComboBox.setSelectedIndex(-1), add ItemListener back to JComboBox (standard and good practicies)

  • create two void (standard and good practicies) in one add listener, in second remove listener

  • don't wrap if(e.getStateChange() == ItemEvent.DESELECTED) inside boolean for the reason, to block code executions untill reset status ended

  • use if - else for if(e.getStateChange() == ItemEvent.DESELECTED){ .... } else { ... }

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top