0

Ok, I'm new to listeners (still learning the language), and this is my first full-scale attempt to implement them (ie more than just a practice problem in a textbook).

So far, everything is working fine except one big bug: the checkboxes don't stay checked. The ItemListener I assign them runs perfectly (I have a JOptionPane set up to trigger to let me know if it's working or not), but the box itself doesn't stay checked.

I went even further and added conditional logic for if it's state is checked versus unchecked, and found that when I click the box BOTH states get triggered. So I get both JOptionPane popups, the one with the message for if the box is checked and the one for if the box isn't checked.

I'm including my code here. What am I doing wrong?

PS. You'll notice that the code has conditional logic to either add a radio button or a checkbox. When the program finally runs, this component is generated in multiple locations in both formats. The radio button works fine, it's the checkbox ones that I'm having the above issue with.

CODE THAT CREATES THE CHECKBOXES AND ASSIGNS THE LISTENERS:

public OtherField(int voteFor){


            this.voteFor = voteFor;


            otherPanel = new JPanel();
            otherPanel.setLayout(new GridLayout(1, 3));


            otherField = new JTextField(10);
            otherField.setHorizontalAlignment(SwingConstants.CENTER);

            JLabel otherLabel;
            otherLabel = new JLabel("Other", SwingConstants.CENTER);

            otherRadio = new JRadioButton("", false);
            otherRadio.setHorizontalAlignment(SwingConstants.CENTER);
            otherRadio.addActionListener(new OtherFieldRadioListener());

            otherCheckBox = new JCheckBox("");
            otherCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
            otherCheckBox.addItemListener(new OtherFieldCheckBoxListener());

            otherPanel.add(otherLabel);
            otherPanel.add(otherField);

            if(voteFor == 1){
                otherPanel.add(otherRadio);
            }else{
                otherPanel.add(otherCheckBox);
            }



        }

LISTENER CODE (it's a private class in the same class as the code above):

private class OtherFieldCheckBoxListener implements ItemListener{
            public void itemStateChanged(ItemEvent e){
                String name = otherField.getText();
                if(e.getStateChange() == ItemEvent.SELECTED){
                    JOptionPane.showMessageDialog(null, name);
                }else{
                    JOptionPane.showMessageDialog(null, "Not Selected");
                }


            }   
        }
camickr
  • 321,443
  • 19
  • 166
  • 288
craigmiller160
  • 5,751
  • 9
  • 41
  • 75
  • 1
    `I'm including my code here` - You included a few random lines of code that may or may not be related to your problem. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. That is start with a JFrame and a JCheckBox and see it works. I'm guessing it will. Then add the ItemListener and see if it still works. Keep adding code until is stops working then you have isolated the code causing the problem. – camickr Mar 21 '15 at 22:19

1 Answers1

0

First thing I would try is to set the checkbox either to true or false when you initialize it, i.e

otherCheckBox.setSelected(false) 

If this does not work I would check whether OtherField gets called from somewhere else everytime the checkbox is selected and thus the components are redrawn/ the selection is reset (use the debugger and set a breakpoint at the beginning of OtherFields)

Ueli Hofstetter
  • 2,409
  • 4
  • 29
  • 52
  • how would I check if it's being called from somewhere else. To give you an idea of my program's structure, all these components should just be created when the window is created. There's nothing that should be causing a redraw, but then again, I wouldn't call myself enough of an expert to rule out having screwed that up. Anyway, what would be the best route to hunt for this? – craigmiller160 Mar 21 '15 at 19:21
  • The easiest way to see whether something is messed up is to use the debugger. Just set a breakpoint at the beginning of OtherField and start the application in debug mode (I edited the answer accordingly). – Ueli Hofstetter Mar 21 '15 at 19:27
  • ok, I tried that, but no luck. Then I tried something else that produced a really weird result: (whoops, need to finish this) So when I pasted my code here, I had it set up so that when a checkbox was clicked, a JOptionPane would appear to say that it had been selected. Since I'm new with listeners, I just wanted to create a very simple action to let me know it was working. Just for the heck of it, I changed the listener to, instead of creating a JOptionPane, change the text of a label instead. And viola, suddenly my checkboxes are working fine. – craigmiller160 Mar 21 '15 at 19:47
  • Is there something about JOptionPane that would cause this conflict? – craigmiller160 Mar 21 '15 at 19:50
  • @user4076806 `make sure you are creating the JOptionPane on the event-dispatch thread` - code executed from within an event listener IS executed on the EDT, so there is not need to use invokeLater() to display the JOptionPane as demonstrated in the code posted here. – camickr Mar 21 '15 at 22:16