1

Hi all I am having some difficulties with adding a joptionpane in JcheckBox listener


public void itemStateChanged(ItemEvent evt) {

            if(evt.getStateChange() == ItemEvent.SELECTED){
                    ///some code

                        JOptionPane.showMessageDialog(null,  "Message", "Alert",
                                JOptionPane.INFORMATION_MESSAGE);
            }
    }

so it works fine,but the problem is that the JCheckBox gets selected and immediately deselected how can I manage to fix this ?

Cheers

greenLizard
  • 2,326
  • 5
  • 24
  • 30
  • 1
    I am curious to know what the "some code" does. Are you sure you're not accedentally changing the state of the checkbox there? – Mia Clarke Mar 28 '10 at 13:16
  • 1
    Which is why a SSCCE: http://sscce.org should be posted with every question, so we don't have to guess what "some code" is doing. – camickr Mar 28 '10 at 15:42

2 Answers2

3

There are a couple of suggestions here (solution) to use an action listener instead of a item listener. This does work, however, I though it strange given that all the texts I have suggest an item listener is the expected type of listener for a check box.

In fact, this is a known bug as acknowledged by Oracle Bug ID:6924233 The JOptionPane apparently causes another event to be generated.

The recommended fix is to invoke the JOptionPane using invokeLater. This works fine and involves only a minor code change to a program already using an item listener for other purposes.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
SteveJP
  • 213
  • 2
  • 8
  • you are right please post this one an answer to my question http://stackoverflow.com/questions/8282488/where-is-lost-setselected-from-jcheckbox , because this post could be closed again +1 – mKorbel Nov 28 '11 at 06:37
2

The problem must be in "///some code" as the following test program works for me in Java 6:

public class CheckBoxItemListener {
    public static void main(String[] args) {
        final JCheckBox checkBox = new JCheckBox("Click me");

        JFrame frame = new JFrame("CheckBox Item Listener");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 300, 300);
        frame.add(checkBox);
        frame.setVisible(true);

        checkBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent evt) {
                if (evt.getStateChange() == ItemEvent.SELECTED){
                    JOptionPane.showMessageDialog(null,  "Message", "Alert",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
    }
}

Have a look in the omitted code for setSelected or doClick calls.

Russ Hayward
  • 5,617
  • 2
  • 25
  • 30
  • Well, it seams that the problem is not in the code I tested my code on my Linux and it works, but I am developing the app on a virtual machine Windows XP and it seams this is the problem. I don't know why but under my XP the code is selecting the checkBox and deselecting it. Does anyone have a clue why? – greenLizard Mar 29 '10 at 12:30