Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what method is executed when I click a JCheckBox

Tags:

java

swing

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.

like image 971
smauel Avatar asked Dec 23 '10 22:12

smauel


1 Answers

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
        }
    }
});
like image 121
Michael Myers Avatar answered Oct 24 '22 09:10

Michael Myers