File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Getting a RadioButton input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting a RadioButton input" Watch "Getting a RadioButton input" New topic

Getting a RadioButton input

Derrick Ritchie

Joined: Jan 27, 2006
Posts: 6
I appreciate this may be really really simple but I`m having a mental block and hope that someone can help.

I am looking to set up a set of JRadioButtons in a ButtonGroup. Its goigng to look like:

RadioButton one
RadionButton two
JButton OK

The user should press the OK button and this should capture whatever is selected in the radion buttons.

However I cant think how to do this, I assume I attach an actionlistener to each Button but surely this would just mean when they click on the OK the window would close and the selection would not be captured as the event would only register the OKButton.

I`ve looked in java.sun but that has not helped.

Any advice, thanks in advance
marc weber

Joined: Aug 31, 2004
Posts: 11343

See if this helps: Java Swing Tutorial: How to use buttons, etc..

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Derrick Ritchie

Joined: Jan 27, 2006
Posts: 6
I have checked that site, I`ve had quite a long look but all that shows is what happens when you click the button. However it does not show what happens if an ok button is added into that.

I`ll try to explain it a bit better. I`m going to set up a class that has universal JRadioButtons yes and no. It also has a universal intger ReturnChoice which returns 1 for yes and 0 for no. If I add an actionlistener to all the buttons will the actionPerformed method be able to change from 1 to 0 depending on which option the user chooses? If the user then clicks on OK when the window closes will this choice be remembered so I can call it using a method like getChoice() which would return an integer value?

I really am only scratching the surface of swing so any helps or good book tips are much appreciated
[ February 25, 2006: Message edited by: Derrick Ritchie ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
see if this is any closer to what you're after

I agree. Here's the link:
subject: Getting a RadioButton input
It's not a secret anymore!