Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to clear  Check Box and Radio Box?

Posted on 2009-03-29
16
393 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!
0
Comment
Question by:duta
  • 8
  • 4
  • 3
  • +1
16 Comments
 
LVL 12

Expert Comment

by:basav_com
ID: 24011905
0
 
LVL 92

Expert Comment

by:objects
ID: 24011924
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.
0
 

Author Comment

by:duta
ID: 24011955
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!

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 92

Expert Comment

by:objects
ID: 24011964
No, you call clearSelection() on the ButtonGroup
0
 

Author Comment

by:duta
ID: 24012086
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
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 24012093
you need to call that on your ButtonGroup object, not the class itself.

 ButtonGroup group = new ButtonGroup();

// add your buttons to the group in some other method

 private  void   clearField  (  )
      {
          group.clearSelection ( );
      }

0
 

Author Comment

by:duta
ID: 24012920

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

0
 
LVL 15

Expert Comment

by:fsze88
ID: 24013109
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();
0
 

Author Comment

by:duta
ID: 24013231


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();
   }
}________________________________________
0
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 250 total points
ID: 24013330
what's your jdk version? code snippet works fine...

C:\Documents and Settings\fsze88\Lb\experts-exchange\java>java -version
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)
import  javax.swing.*;
import  java.awt.*;
import java.awt.event.MouseAdapter;
 
import java.awt.event.MouseEvent;
 
 
public class test extends JFrame
{
    private JPanel panelCenter = new JPanel ();
    ButtonGroup bg = new ButtonGroup() ;
    JCheckBox jCheckBox1 = new JCheckBox();
    JCheckBox jCheckBox2 = new JCheckBox();
    private JButton jButton1 = new JButton();
    
  public test(){
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    jButton1.setText("testing");
    jButton1.setBounds(new Rectangle(100,   100, 50, 50));
 
    jButton1.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            bg.clearSelection();
        }
    });
    bg.add(jCheckBox1);
    bg.add(jCheckBox2);
 
    jCheckBox1.setBounds(new Rectangle(200, 200, 50, 50));
    jCheckBox2.setBounds(new Rectangle(300, 200, 50, 50));
 
    panelCenter.add(jCheckBox1, null);
    panelCenter.add(jCheckBox2, null);
    panelCenter.add(jButton1, null);
    this.getContentPane().add( panelCenter);
 
    this.setSize(new Dimension(800, 600));
    this.setVisible(true);
    
  
  }
   public static void main ( String [] args)
   {
    new test();
   }
}

Open in new window

0
 

Author Comment

by:duta
ID: 24013350

I use the following version:

jdk  1.5.0_09
jre 1.5.0   _07
             

Thanks a lot!
0
 

Author Comment

by:duta
ID: 24013354
When I tested your latest, I got the same error.

I guess I need to upgrade jdk to 1.6.

0
 

Author Comment

by:duta
ID: 24013357
I wonder what version of JDK or JRE  you use.
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24013360
yes, I think so
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24013394
jre 1.6.0.12, jdk 1.6.0.12
after updated jdk to the lastest version.It should updated with jre automatically...
0
 

Author Closing Comment

by:duta
ID: 31563997
Thank you very much, master and savant!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question