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);  
}
user2175095
  • 71
  • 1
  • 1
  • 5
  • As I can think "checked" is property so you need to set it as optionC.Checked = false; but not sure – Senad Meškin Mar 25 '13 at 16:18
  • 3
    Show your complete code.. – Vishal K Mar 25 '13 at 16:19
  • Provide a compilable example that reproduces your problem. – Jean Waghetti Mar 25 '13 at 16:21
  • If you have registered some `Listener` to `JCeckBoxes` show the associated actions also.. – Vishal K Mar 25 '13 at 16:25
  • @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 button – user2175095 Mar 25 '13 at 16:26
  • 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. – camickr Mar 25 '13 at 16:27
  • `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. – camickr Mar 25 '13 at 16:28
  • @camickr My bad. I made some silly typos. Does this work? – user2175095 Mar 25 '13 at 16:34
  • Have you registered any type of `Listener` to those checkboxes? – Vishal K Mar 25 '13 at 16:48
  • @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? – camickr Mar 25 '13 at 16:54
  • @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. – camickr Mar 25 '13 at 16:56
  • @camickr 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.. – Vishal K Mar 25 '13 at 16:57
  • @VishalK, fair enough ;) I guess we are all wasting time since this is such a poor question with little information to go on. – camickr Mar 25 '13 at 17:00
  • @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 ? – Vishal K Mar 25 '13 at 17:03
  • @VishalK, anything is possible... – camickr Mar 25 '13 at 17:10
  • @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.. – Vishal K Mar 25 '13 at 17:16

5 Answers5

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

Ahuramazda
  • 427
  • 9
  • 14
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.

Freakz0r
  • 53
  • 1
  • 12
0

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

checkbox.toggle();
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
0

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

checkboxGroup1.setSelectedCheckbox(null);
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
-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.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • 1
    -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. – camickr Mar 25 '13 at 16:52
  • @PeterMmm is there an alternative way in which I can uncheck the checkboxes? – user2175095 Mar 25 '13 at 16:54
  • 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. – Joshua Goldberg Jan 09 '14 at 19:41
  • 1
    @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*. – Marco13 Aug 28 '14 at 20:21
  • @Marco13 Looking back at that code, I'm almost certain that was the problem, thanks for the comment. – Joshua Goldberg Sep 03 '14 at 15:46