0

I have created a toolbar with a spinner and a checkbox.

The spinner works as I expect. When I change the value, the listener is called AND the spinner value is CHANGED.

BUT, for checkbox, the listener is fired when I move over it (unlike spinner) AND ODDLY checked value does NOT GET CHANGED unless I remove the JOptionPabe statement. How should I write CheckBox?

final JSpinner spin2 = new JSpinner();
        spin2.setModel(new SpinnerNumberModel(10, 1, 3000, 1));
        ChangeListener listener2 = new ChangeListener() {       
            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                JOptionPane.showMessageDialog(null,"Spinner change");
                //show();
            }
            };
        spin2.addChangeListener(listener2);
        toolBar.add(spin2);

    final JCheckBox AlertAudible = new JCheckBox("Audible");
        AlertAudible.setSelected(true);
        AlertAudible.setText("Audible");
        AlertAudible.setForeground(Color.WHITE);
        ChangeListener listener4 = new ChangeListener() {       
            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub

                JOptionPane.showMessageDialog(null,"Alert check");
                //starterModule.sigmaAlertAudible = AlertAudible.isSelected();
                //show();
            }
            };

OK I changed it to an ItemListener:

    ItemListener listener4 = new ItemListener() {
        @Override
            public void itemStateChanged(ItemEvent e) {


                if (e.getStateChange() == ItemEvent.SELECTED) {
                    JOptionPane.showMessageDialog(null,"Alert check SELECTED :"+e.getStateChange());

                }
                if (e.getStateChange() == ItemEvent.DESELECTED){
                    JOptionPane.showMessageDialog(null,"Alert check DESELECTED :"+e.getStateChange());


                }
            }
        }

ODDLY my JOptionPane gets called TWICE everytime I "Check" the box! Both SELECTED and DESELECTED. SO value remains the same!

alex2410
  • 10,904
  • 3
  • 25
  • 41
ManInMoon
  • 6,795
  • 15
  • 70
  • 133

2 Answers2

1

You need to use ItemListener as recommended, also use next trick with SwingUtilities.invokeLater(), and your JOptionPane will be called only once:

ItemListener listener4 = new ItemListener() {
        @Override
        public void itemStateChanged(final ItemEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        JOptionPane.showMessageDialog(null,"Alert check SELECTED :"+ e.getStateChange());
                    } else if (e.getStateChange() == ItemEvent.DESELECTED) {
                        JOptionPane.showMessageDialog(null,"Alert check DESELECTED :"+ e.getStateChange());
                    }
                }
            });

        }
    };

seems it was a bug

alex2410
  • 10,904
  • 3
  • 25
  • 41
0

I think you need an ItemListener, not a ChangeListener. See the example here: http://docs.oracle.com/javase/tutorial/uiswing/components/button.html

ItemListener listener4 = new ItemListener() {

        public void itemStateChanged(ItemEvent e) {

            if (e.getStateChange() == ItemEvent.SELECTED) {
                JOptionPane.showMessageDialog(null,"Alert check :"+e.getStateChange());
            }
        }
    };


    AlertAudible.addItemListener(listener4);
hankd
  • 649
  • 3
  • 12
  • But that example means I have to set the checked value myself! Is that right - because for spinner it's done for you? – ManInMoon Nov 19 '13 at 17:20
  • Make it addItemListener instead of addChangeListener, see if that fixes it. – hankd Nov 19 '13 at 17:26
  • Also, you probably only want to do it if the ItemEvent is that the box was selected, see my edit to the answer. – hankd Nov 19 '13 at 17:28
  • It seems creating a MessageDialog is automatically de-selecting the checkbox. I'm still not sure what the desired behavior of the program is for you? If you want to do something when the user checks the box, then do it in the if(e.getStateChange() == ItemEvent.SELECTED) block, but not anywhere else. – hankd Nov 19 '13 at 17:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41483/discussion-between-hank-ditton-and-maninmoon) – hankd Nov 19 '13 at 17:49