Solved

Clearing a selection in a Checkboxgroup

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 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

24 Experts available now in Live!

Get 1:1 Help Now