1

I have a group of JRadioButtons and a single JCheckBox. If the JCheckBox is unchecked, the JRadioButtons should disable and reset, and vice versa. The problem I have is whether I check the JCheckBox or not, the JRadioButtons stay disabled.

Also before going on to the code, don't mind the null layout and absence of different classes. I quickly made a test project to reduce the amount of code I would have to paste here.

package test;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 434, 261);
        frame.getContentPane().add(panel);

        JCheckBox ckbxTestCheckBox = new JCheckBox("Test Check Box");
        ckbxTestCheckBox.setBounds(7, 7, 99, 23);
        panel.add(ckbxTestCheckBox);
        JRadioButton rdbtnTestRadioButton1 = new JRadioButton("Test Radio Button 1");
        rdbtnTestRadioButton1.setBounds(7, 34, 121, 23);
        panel.add(rdbtnTestRadioButton1);
        JRadioButton rdbtnTestRadioButton2 = new JRadioButton("Test Radio Button 2");
        rdbtnTestRadioButton2.setBounds(7, 61, 121, 23);
        panel.add(rdbtnTestRadioButton2);
        JRadioButton rdbtnTestRadioButton3 = new JRadioButton("Test Radio Button 3");
        rdbtnTestRadioButton3.setBounds(7, 88, 121, 23);
        panel.add(rdbtnTestRadioButton3);

        JRadioButton rdbtnTest[] = {rdbtnTestRadioButton1, rdbtnTestRadioButton2, rdbtnTestRadioButton3};

        ButtonGroup btnGrpTest = new ButtonGroup();
        for(int i = 0; i < rdbtnTest.length; i++){
            btnGrpTest.add(rdbtnTest[i]);
        }

        if(!ckbxTestCheckBox.isSelected()){
            for(int i = 0; i < rdbtnTest.length; i++){
                rdbtnTest[i].setEnabled(false);
                rdbtnTest[i].setSelected(false);
            }
        } else {            //Is this part even necessary?
            for(int i = 0; i < rdbtnTest.length; i++){
                rdbtnTest[i].setEnabled(true);
            }
        }
    }
}
Maikyl
  • 35
  • 6
  • 2
    Where do you add checkbox action listener that enables/disables radio buttons accordingly? If nowhere - then that's you problem. – zubergu Jun 06 '16 at 14:33
  • 2
    Read the section from the Swing tutorial on [How to Use Check Boxes](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) for working examples of using an ActionListener. Also, you should NOT be defining all your code in the main() method. The `CheckBoxDemo` code found in the tutorial will show you how to better structure your code to follow. – camickr Jun 06 '16 at 14:38
  • @zubergu Thanks! I was indeed missing an ItemListener/ActionListener. – Maikyl Jun 06 '16 at 14:47
  • @camickr Please read the full question before pointing out what code should go where. ;) – Maikyl Jun 06 '16 at 14:49
  • 1
    @Maikyl, any code you post here should still be proper code, if you expect people to take the time to read the code. In fact is it easier to create proper code. For example you should not be using setBounds(). It is far easier to just simple use the add() method and let the FlowLayout do its job then randomly guess what the size of each component should be. – camickr Jun 06 '16 at 14:56

1 Answers1

1

As @zubergu pointed out, your logic must be written inside an ItemListener for the checkbox , or it makes no sense.

Furthermore, your logic can be quite simplified without if and else blocks :

   ckbxTestCheckBox.addItemListener(new ItemListener() {
          public void itemStateChanged(ItemEvent e) {
                for(int i = 0; i < rdbtnTest.length; i++){

                    rdbtnTest[i].setEnabled(!ckbxTestCheckBox.isSelected());

                    if(!ckbxTestCheckBox.isSelected()) 
                       rdbtnTest[i].setSelected(false);
                }

          }
        });

Note that for JCheckBox, an ActionListener instead of an ItemListener, would also work.

Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • 1
    Thanks a lot for this. It's funny how you can break your head on a bit of code when the answer is so obvious... I was indeed missing an ItemListener. – Maikyl Jun 06 '16 at 14:46
  • 1
    *"when the answer is so obvious..."* We've all experienced it. A second (3rd etc..) pair of eyes can come in real handy for those situations. – Andrew Thompson Jun 06 '16 at 15:00