user2175095
user2175095

Reputation: 71

uncheck checkboxes in java

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);  
}

Upvotes: 7

Views: 74660

Answers (5)

jupeteramar
jupeteramar

Reputation: 1

I tried using the this method for the checkbox group with a null parameter:

checkboxGroup1.setSelectedCheckbox(null);

Upvotes: 0

Inzimam Tariq IT
Inzimam Tariq IT

Reputation: 6748

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

checkbox.toggle();

Upvotes: 0

Freakz0r
Freakz0r

Reputation: 53

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.

Upvotes: 1

Ahuramazda
Ahuramazda

Reputation: 427

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

Upvotes: 11

PeterMmm
PeterMmm

Reputation: 24630

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.

Upvotes: -1

Related Questions