This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JRadioButtons w/a JButton

 
Sean Richards
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
max
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...



Enjoy,

-Nate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic