uncheck checkboxes in java

蓝咒 提交于 2020-01-01 06:07:06

问题


In my program, I want to uncheck all the checkboxes whenever this method is called. Can someone explain why it isn't working? Whenever I call this method the 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);  
}

回答1:


first of all you need to bring all of the check box s code on the top of your for example state change method & after that for uncheck the check box you can make a variable like state & put the variable value on false & after that you can call the checkbox.setSelected(false); or boolean state = false; CheckBox.setSelected(state);that's it !!!




回答2:


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

buttonGroup1.clearSelection();

After trying almost every method. This one is by far the easiest and the most efficient one.




回答3:


If you'r sure checkbox are checked, You can toggle them.

checkbox.toggle();



回答4:


In general in Swing any change made in the backend is not propagated to the visual elements. One good known exception is JTextField.setText() (any call to setText will update the visual text element immediatly).

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

You may stay with your code but then you have to (in)validate the container.



来源:https://stackoverflow.com/questions/15619682/uncheck-checkboxes-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!