How to check that a JCheckBox is checked?

JavaSwingJcheckbox

Java Problem Overview


How can I check if a JCheckBox is checked?

Java Solutions


Solution 1 - Java

Use the isSelected method.

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

Solution 2 - Java

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_}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiononeatView Question on Stackoverflow
Solution 1 - JavaMatthew FlaschenView Answer on Stackoverflow
Solution 2 - Java1ac0View Answer on Stackoverflow