开发者

How to disable Checkbox depending on another Checkbox?

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 setE开发者_JAVA技巧ditable(false) or something.


JCheckBox.setEnabled(false)

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


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


yourCheckBox.setEnabled(false);


or you can use ButtonGroup:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜