I have a couple of radio button groups for different things like entrees, appetizers, and drinks. I want the user to be able to select one of each type of thing.
I’m not sure how to implement the actions listeners though – does each group need its own action listener? If so, how do I differentiate between action listeners/groups?
private class SelectionChangeMade implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String drinkChoice = drinkButtonGroup.getSelection().getActionCommand();
System.out.println(drinkChoice);
// String appetizerChoice =
appetizerButtonGroup.getSelection().getActionCommand();
// above line gives me a NullPointerError if uncommented
}
}
No you don’t need to create one action listener per button group. You can use the action listener you wrote to read the user input.
If you are trying to get the radio button selection on a particular event like user clicking submit button, then you add this action listener to submit button.
And, the Null Pointer Exception is caused by user not selecting any appetizer, you have to make sure that user has selected some appetizer before getting the action command for it.