Solved

How to null out-reset JRadioButtons!

Posted on 2004-10-02
15
535 Views
Last Modified: 2010-03-31
I have a Java swing application and I can't seem to null out the radio buttons (JRadioButtons) after it performs a reset - the syntax for this section is the following where years, monthly and amount are my JButtonGroups! The actually radiobuttons are called rad1, rad2, rad3, rad4, rad4, rad6, rad7, rad8, rad9 as each buttongroup has three radio buttons assigned to it

if(source == button2){
total = 0; //clear total or else the value will stay for next calculation
//set radio buttons to unchecked also
years = 0;
monthly = 0;
amount = 0;

                        textLabel5.setText("");
                  }
0
Comment
Question by:kpu8
  • 7
  • 6
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Which ones do you need to deselect (there has to be one in each group selected)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
add an extra radio button to the group and make it invisible.
to reset, select that radio button.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
to make it invisible simply don't add it to your container (but do add it to your button group)
0
 
LVL 1

Author Comment

by:kpu8
Comment Utility
CEHJ - The request was to make them all deselected!

objects - Any examples would be great of how to do this - I'm a little unclear how to make a button invisible!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>CEHJ - The request was to make them all deselected!

Well that's not how radio buttons are meant to work - checkboxes yes, radio buttons no ;-)
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
Comment Utility
>  I'm a little unclear how to make a button invisible!

just don't add it to your container

JRadioButton a = new JRadioButton("a");
panel.add(a);
group.add(a);
JRadioButton b = new JRadioButton("b");
panel.add(a);
group.add(a);
JRadioButton deselect = new JRadioButton("");
group.add(deselect );


then to deselect all use:

deselect.setSelected(true);
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
The code I posted demonstrates how to null out a group of radio buttons as asked.
And is the accepted way to achieve what is required.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
And i was just pointing out that kpu8 should know that's not how radio buttons are meant to work and that making them work like that is simply bad gui design
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Have a set of radio buttons with none selected is a perfectly valid state.
The question doesn't ask how radio buttons are meant to work, it ask how to reset them which I have answered.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Have a set of radio buttons with none selected is a perfectly valid state.

No - it is not
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
When you first create an rb it is unselected, ie. its a valid state.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
That's a different issue. A single radio button is in any case a theoretical entity in a gui
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Why would I be talking about a single radio button :-D
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I don't know but you were unless 'an rb' is plural in your world ..
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 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

10 Experts available now in Live!

Get 1:1 Help Now