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);
精彩评论