开发者

How to check that a JCheckBox is checked?

开发者 https://www.devze.com 2023-01-14 07:18 出处:网络
How can I check if a JCheckBox is开发者_如何学编程 checked?Use the isSelected method. You can also use an ItemListener so you\'ll be notified when it\'s checked or unchecked.By using itemStateChanged

How can I check if a JCheckBox is开发者_如何学编程 checked?


Use the isSelected method.

You can also use an ItemListener so you'll be notified when it's checked or unchecked.


By using itemStateChanged(ItemListener) you can track selecting and deselecting checkbox (and do whatever you want based on it):

myCheckBox.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getStateChange() == ItemEvent.SELECTED) {//checkbox has been selected
            //do something...
        } else {//checkbox has been deselected
            //do something...
        };
    }
});

Java Swing itemStateChanged docu should help too. By using isSelected() method you can just test if actual is checkbox selected:

if(myCheckBox.isSelected()){_do_something_if_selected_}
0

精彩评论

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