Solved

Clearing a selection in a Checkboxgroup

Posted on 2001-06-28
3
264 Views
Last Modified: 2006-11-17
I have a checkboxgroup, myGroup with individual boxes male, female.

I want a "clear" function to reset the choice to null. Nothing works. setState(false) does not work for me on myGroup or male, female.

How can I do that?
0
Comment
Question by:HanRui
  • 2
3 Comments
 
LVL 1

Expert Comment

by:eattrig
ID: 6236174
Try this out, this should be compilable:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Test
{
    CheckboxGroup myGroup = new CheckboxGroup();

    public Test()
    {
        Checkbox male = new Checkbox("Male", myGroup, false);
        Checkbox female = new Checkbox("Female", myGroup, false);

        male.setFont(new Font("Dialog", 1, 14));
        male.setLocation(20, 10);
        male.setSize(80, 30);

        female.setFont(new Font("Dialog", 1, 14));
        female.setLocation(20, 40);
        female.setSize(80, 30);

        JButton button = new JButton("Deselect All");
        button.setLocation(10, 80);
        button.setSize(100, 45);
        button.setBorder(new BevelBorder(BevelBorder.RAISED));
        button.addActionListener
        (
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    // this is where you set the selected
                    // checkbox to null, or none
                    myGroup.setSelectedCheckbox(null);
                }
            }
        );

        JFrame frame = new JFrame();
        frame.setLocation(300, 300);
        frame.setSize(120, 155);
        frame.setResizable(false);
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(male);
        frame.getContentPane().add(female);
        frame.getContentPane().add(button);
        frame.setVisible(true);
    }
    public static void main(String[] args)
    {
        new Test();
    }
}

Hope this helps.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 6236426
Add a 'hidden' checkbox (ie. not visible) to your group and set that checkbox. This will clear the state of the other two.
0
 
LVL 92

Expert Comment

by:objects
ID: 6236436
Thanks for the points :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now