How to disable Checkbox depending on another Checkbox?

30,380

Solution 1

JCheckBox.setEnabled(false)

A tutortial showing exactly that is here: How to Use Buttons, Check Boxes, and Radio Buttons

Solution 2

Something like this?

 final JCheckBox a = new JCheckBox();
 final JCheckBox b = new JCheckBox();
 a.addItemListener(new ItemListener() {
  @Override
  public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED){
      b.setEnabled(a.isSelected());
    }
  }
 });

Solution 3

or you can use ButtonGroup:

JCheckBox chkA = new JCheckBox();
JCheckBox chkB = new JCheckBox();
ButtonGroup group = new ButtonGroup();
group.add(chkA);
group.add(chkB);

Solution 4

yourCheckBox.setEnabled(false);

Share:
30,380
razshan
Author by

razshan

Updated on July 17, 2022

Comments

  • razshan
    razshan almost 2 years

    On Java, is there any way to disable a checkbox (call it B), if checkbox A is checked.

    When I say disable, the user can't check it off..Its setEditable(false) or something.