We help IT Professionals succeed at work.

How to clear  Check Box and Radio Box?

duta
duta asked
on
411 Views
Last Modified: 2012-05-06
Dear experts:

I would like to learn how to clear  Radio Buttons and Check Boxes when "Clear" button is clicked.

Thanks!
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you can use the clearSelection() method in ButtonGroup
pre-1.6 for radio buttons you can use an extra hidden radio button, and select it to clear the selected one.

Author

Commented:
Thank you so much, Master and Savant.

TO: SAVANT:

Suppose that there are a radio button named "radioBtn" and a check box named checkChb".

To clear them, should I use:

radioBtn.clearSelection ( );
checkChb.clearSelection ( );

TO: MASTER:
I checked http://www.rgagnon.com/javadetails/java-0221.html.
It is a little complicated. But I will take time to read the  tip available at the site again.

Thanks!

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
No, you call clearSelection() on the ButtonGroup

Author

Commented:
I have a code like this:
 private  void   clearField  (  )
      {
         ;
          ButtonGroup.clearSelection ( );
      }

When I ran the code, I got an error message as the follows:
cannot-find.jpg
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:

Savant, I still get an error message as below:

ERROR: cannot find symbol
Symbol:  method  clearSelection
location:  class   javax.swing.ButtonGroup


Thanks a lot!


__________________ My Current Code ______________
import    javax.swing.ButtonGroup;

private    ButtonGroup    RadioButtonButtonGroup;
private    ButtonGroup   CheckBoxButtonGroup

RadioButtonButtonGroup  =  new  ButtonGroup (  );
CheckBoxButtonGroup    =  new  ButtonGroup (  );

.
.
private    void   clearField ( )
{
    RadioButtonGroup.clearSelection ( );
   CheckBoxButtonGroup.clearSelection ( );
}

CERTIFIED EXPERT

Commented:
I tested this, works

    private ButtonGroup bg = new ButtonGroup() ;
    private JCheckBox jCheckBox1 = new JCheckBox();
    private JCheckBox jCheckBox2 = new JCheckBox();
        bg.add(jCheckBox1);
        bg.add(jCheckBox2);
        bg.clearSelection();

Author

Commented:


I created a simple class (as below) to test your code.  I still got the same error:

cannot find symbol
symbol: method clearSelection ( )

I just wonder what classes you imported.



___________ My test code ________
import  javax.swing.*;
import  java.awt.*;

public class test extends JFrame
{
   public static void main ( String [] args)
   {

    ButtonGroup bg = new ButtonGroup() ;
    JCheckBox jCheckBox1 = new JCheckBox();
    JCheckBox jCheckBox2 = new JCheckBox();
        bg.add(jCheckBox1);
        bg.add(jCheckBox2);
        bg.clearSelection();
   }
}________________________________________
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:

I use the following version:

jdk  1.5.0_09
jre 1.5.0   _07
             

Thanks a lot!

Author

Commented:
When I tested your latest, I got the same error.

I guess I need to upgrade jdk to 1.6.

Author

Commented:
I wonder what version of JDK or JRE  you use.
CERTIFIED EXPERT

Commented:
yes, I think so
CERTIFIED EXPERT

Commented:
jre 1.6.0.12, jdk 1.6.0.12
after updated jdk to the lastest version.It should updated with jre automatically...

Author

Commented:
Thank you very much, master and savant!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.