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

JRadioButtons w/a JButton

Sean Richards

Joined: Feb 02, 2002
Posts: 8
i'm working on a project that requires 4 radio buttons and a jbutton to process the info. How do I set it up so that when a user chooses one of the radio buttons and hit the jbutton, the dialog box will alert the user to which radio button was selected?
I have to use radio buttons!
thanks in advance.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

The way I would do it is register a listener on your radio buttons, then, you could either set a boolean value to true or false, depending on which one is selected, OR you could set a String value to the value of the chosen radio button.
Either way, when you click the JButton, just check either the boolean or the String.
BTW - Someone is going to tell you to make your name compliant with JavaRanch's naming policy. Maybe you could beat them to it.
[ May 07, 2002: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

First, maximillion2001-
Welcome to the JavaRanch! Just as Gregg mentioned... You need to adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Now, on to your question... The preferred way to deal with JRadioButtons is to put them into a ButtonGroup... ButtonGroups handle all the logic that says that only one JRadioButton at a time is selected. Since ButtonGroups return the selected ButtonModel and ButtonModel only uses action commands, you will have to set the action command of your JRadioButton. Then you just ask your ButtonGroup for the selected ButtonModel in the ActionListener for your JButton...

Here's some code to illustrate how to use a ButtonGroup to achieve this...



Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: JRadioButtons w/a JButton
It's not a secret anymore!