Reputation: 11
I have two butons to select gender,and I want to check if the user selected one of them, I want to do that with try and catch but I don't excatly know how to do it. Gender in database is char . Can someone help me???
MButton.setActionCommand("Male");
FButton.setActionCommand("Female");
s.setGender(buttonGroup1.getSelection().getActionCommand().charAt(0))
Upvotes: 1
Views: 200
Reputation: 324207
First of all:
Variable names should NOT start with an upper case character. Some of your variables are correct. Others are not. Be consistent!!!
The "action command" defaults to the text on the button, so assuming you use "Male" and "Female" as the text of each radio button you don't need to set the "action command" directly.
I have two butons to select gender,and I want to check if the user selected one of them.
Well you need to know when to do this check. Presumably you have some kind of "Save" button that the user clicks to process all the data that has been entered on the form. So in the ActionListener for this button you would add logic to validate the selection of one of the radio buttons.
Something like:
ButtonModel model = buttonGroup1.getSelection();
if (model != null)
{
s.setGender(model.getActionCommand().charAt(0))
}
else
{
//error
}
Or the other approach is to just add an ActionListener to each radio button. When the button is selected you set the "gender" value. Then when you click the "Save" button you check if the "gender" property has a value. If not, do your error processing.
Upvotes: 1
Reputation: 4266
You probably don't need a try...catch
, just use a simple if
statement (or two):
if (radioButton1.isSelected()) {
// do something
}
else if (radioButton2.isSelected()) {
// do something else
}
Upvotes: 0