I have written a class that extends JCheckBox and am now looking 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
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With