views:

90

answers:

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

+2  A: 

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
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?
Riddick