开发者

what method is executed when I click a JCheckBox

开发者 https://www.devze.com 2023-01-31 20:32 出处:网络
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\', b

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

精彩评论

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