user1210061
user1210061

Reputation:

Retrieving data from Radio Buttons

I need to retrieve data from radio buttons.

Basically like this:

String Gender = rdbtnM.getText();

My problem is, the user can select from one of 2 radio buttons: rdbtnM and rdbtnF. So the String Gender should have the value whatever has been chosen; F or M.

How do I write that? I tried this, but doesn't work:

String Gender - rdbtnM.getText(); && rdbtnF.getText();

Upvotes: 0

Views: 4380

Answers (3)

ujjwol shrestha
ujjwol shrestha

Reputation: 155

if(radiomale.isSelected())
String gender="male";
if(femaleradio.isSelected())
String gender="female";

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

Don't give the radio buttons an ActionListener. One way to get the result is to query the ButtonGroup that controls the RadioButton. If you've given each JRadioButton an appropriate actionCommand then the ButtonModel returned by the ButtonGroup will hold that String. For example, please have a look at the sample code here.

Upvotes: 4

chris.amery
chris.amery

Reputation: 1

If the variable is global, implement an action listener to both radio buttons that triggers when a user toggles either one. Within the action listener method, set the string Gender to the appropriate value as required.

Upvotes: 0

Related Questions