what method is executed when I click a JCheckBox
I have written a class that extends JCheckBox and am now loo开发者_开发问答king to override the method that gets executed when the check box is clicked. I have tried 'setSelected', and 'doClick', but neither do as I expect.
Any help is greatly appreciated.
It's an event-driven model; what you need to do is attach an ItemListener to the checkbox.
See the Swing Tutorials: How to use check boxes.
Your code might look something like this:
...
myCheckBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
// the checkbox was just selected
} else {
// the checkbox was just deselected
}
}
});
精彩评论