uncheck checkboxes in java
Asked Answered
G

5

7

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);  
}
Goodsized answered 25/3, 2013 at 16:17 Comment(16)
As I can think "checked" is property so you need to set it as optionC.Checked = false; but not sureLepsy
Show your complete code..Markswoman
Provide a compilable example that reproduces your problem.Stanislaw
If you have registered some Listener to JCeckBoxes show the associated actions also..Markswoman
@JeanWaghetti I'm new to this, but I think by compilable you mean something you can run on ur computer. hmm I would just add 4 checkboxes to a UI and a button to a jFrame and call the clearOptions() method in the event of the buttonGoodsized
First you say uncheck the all textboxes , then you say the checkboxes remain selected and finally in your comment in the code you say Make sure the radio buttons are not checked. So what is it? You reference 3 different components (although I still don't know what a "textbox" is). If you want help then take the time to ask a well defined and clear question.Myrtamyrtaceous
I would just add 4 checkboxes to a UI and a button to a jFrame and call the clearOptions() method in the event of the button . Exactly, so post the code that you wrote to do this. We don't have the time to write the code for you. It's called an SSCCE. If you don't know this term then search the web.Myrtamyrtaceous
@Myrtamyrtaceous My bad. I made some silly typos. Does this work?Goodsized
Have you registered any type of Listener to those checkboxes?Markswoman
@VishalK, registering a listener has nothing to do with painting of the component in a selected or unselected state. Maybe you meant to say did you add an ActionListener to the button?Myrtamyrtaceous
@user2175095, Does this work? Did you read up on a SSCCE? Obviously not. How does that code compile? Where do you create the components and add them to the frame? Did you add a println(..) staement in your method to see if the code is even being invoked.Myrtamyrtaceous
@Myrtamyrtaceous Why I asked it because if OP did some manipulation in states of other OptionsX on Changing the state of OptionA like making all as selected true if OptionA is unSelected.. That might lead to such problem as well..Markswoman
@VishalK, fair enough ;) I guess we are all wasting time since this is such a poor question with little information to go on.Myrtamyrtaceous
@camickr: yeah you are right ;) Wastage of time and words....But do you think that whatever situation I mentioned in my last comment is also one of the possibilities for the failure of his code ?Markswoman
@VishalK, anything is possible...Myrtamyrtaceous
@camickr: Thanks for this positive words.. I am a big admirer of you and I read your blogs on Swing and almost every answer that you write here :) . I learn many things from you. God bless you..Markswoman
K
11

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

Krauss answered 27/9, 2013 at 9:20 Comment(0)
B
1

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.

Beaconsfield answered 8/3, 2016 at 20:57 Comment(0)
S
0

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

checkbox.toggle();
Sapper answered 11/2, 2018 at 9:28 Comment(0)
T
0

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

checkboxGroup1.setSelectedCheckbox(null);
Troudeloup answered 21/4, 2021 at 4:31 Comment(0)
C
-1

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.

Country answered 25/3, 2013 at 16:34 Comment(5)
-1, Swing automatically repaints the component when a property of the component is changed. Invoking the setSelected(...) method will cause the component to repaint itself. There is no need for validate(). That might be an old AWT requirement, but it is not needed with Swing.Myrtamyrtaceous
@Country is there an alternative way in which I can uncheck the checkboxes?Goodsized
I have a counterexample. JCheckBoxes in a Box in a JDialog, with calls to setSelected. It would sometimes get into incorrectly painted state (checkbox states were correct, but drawn wrong) until I added an ItemListener that called repaint on the dialog. That fixed it. Assuming I find time I'll post and link here to a question with a SSCCE for further analysis.Cemetery
@JoshuaGoldberg This might happen if you call cb.setSelected(state) from a thread that is not the Event Dispach Thread. Such a modification (like all actions on Swing Components) should only be done on the Event Dispatch Thread.Belligerent
@Belligerent Looking back at that code, I'm almost certain that was the problem, thanks for the comment.Cemetery

© 2022 - 2024 — McMap. All rights reserved.