Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that a JCheckBox is checked?

How can I check if a JCheckBox is checked?

like image 548
oneat Avatar asked Aug 30 '10 11:08

oneat


People also ask

Which method of JCheckBox tells you if a checkbox is checked Mcq?

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

How do you check if a checkbox is checked or not in Java Swing?

Use addActionListener or addItemListener() so that a method will be called whenever the checkbox is changed. Passive. Use isSelected() to test if a checkbox is checked.

Which method is used to check the status of checkbox?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.


2 Answers

Use the isSelected method.

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

like image 60
Matthew Flaschen Avatar answered Sep 19 '22 09:09

Matthew Flaschen


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_} 
like image 40
1ac0 Avatar answered Sep 23 '22 09:09

1ac0