3

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.

robi-y
  • 1,687
  • 16
  • 25
razshan
  • 1,128
  • 12
  • 41
  • 59

4 Answers4

10

JCheckBox.setEnabled(false)

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

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
5

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());
    }
  }
 });
Arthur Thomas
  • 5,088
  • 1
  • 25
  • 31
2

yourCheckBox.setEnabled(false);

2

or you can use ButtonGroup:

JCheckBox chkA = new JCheckBox();
JCheckBox chkB = new JCheckBox();
ButtonGroup group = new ButtonGroup();
group.add(chkA);
group.add(chkB);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417