Search code examples
javaswingjcheckboxjradiobutton

JCheckBox enables/disables JRadioButton


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);
            }
        }
    }
}

Solution

  • 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.