This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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 ]
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com