Solved

Clearing a selection in a Checkboxgroup

Posted on 2001-06-28
3
270 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
[X]
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
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

687 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