uncheck checkboxes in java

后端 未结 5 901
借酒劲吻你
借酒劲吻你 2021-01-02 17:04

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

相关标签:
5条回答
  • 2021-01-02 17:25

    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.

    0 讨论(0)
  • 2021-01-02 17:25
     public void clearOptions ()   
    { 
                checkbox1.setState(false); 
                 checkbox2.setState(false);
                 checkbox3.setState(false);
                 checkbox4.setState(false);
    }
    
    0 讨论(0)
  • 2021-01-02 17:27

    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 !!!

    0 讨论(0)
  • 2021-01-02 17:30

    If you are sure checkbox is checked you can toggle it.

    checkbox.toggle();
    
    0 讨论(0)
  • 2021-01-02 17:39

    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.

    0 讨论(0)
提交回复
热议问题