Clear checkboxes in java

In my program, I want to clear all the checkboxes whenever this method is called. Can someone explain why it is not working? Whenever I call this method, checkboxes are still selected.

private void nextQuestionButtonActionPerformed(java.awt.event.ActionEvent evt) { clearOptions(); } public void clearOptions () { //Make sure the check boxes are not checked optionA.setSelected(false); optionB.setSelected(false); optionC.setSelected(false); optionD.setSelected(false); } 
+4
source share
4 answers

First of all, you need to bring all the code to the top of your, for example, state change method, and after that, to uncheck the box, you can make a state variable and put the value of the variable on false and after that you can call checkbox.setSelected(false); or boolean state = false; CheckBox.setSelected(state); boolean state = false; CheckBox.setSelected(state); , what is it!!!

+8
source

The easiest way to do this is to apply the same button group to all of your checkboxes. And then just use:

 buttonGroup1.clearSelection(); 

Try almost every method. This one is by far the easiest and most effective.

0
source

If you are sure that the checkbox checked, you can switch them.

 checkbox.toggle(); 
0
source

In general, in Swing, any change made to the backend does not apply to visual elements. One well-known exception is JTextField.setText() (any call to setText will immediately update the visual text element).

It is even documented in an API document: http://docs.oracle.com/javase/6/docs/api/javax/swing/AbstractButton.html#setSelected(boolean) .

You can stay with the code, but then you should (at) check the container.

-1
source

All Articles