Solved

Clearing a selection in a Checkboxgroup

Posted on 2001-06-28
3
267 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
Way to decrease size of apk file 9 86
How to convert String matching to regex in java 4 48
How to determine if a string is a valid SHA value 7 35
Java Eclipse Loop 3 20
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)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

789 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