Determining the Selected JRadioButton in a Button Group

When you ask a button group for the currently selected radio button, it returns the selected radio button's model (rather than the selected radio button itself). Fortunately, the button group maintains the list of buttons and so you can iterate over this list looking for one with the same model.
// This method returns the selected radio button in a button group public static JRadioButton getSelection(ButtonGroup group) { for (Enumeration e=group.getElements(); e.hasMoreElements(); ) { JRadioButton b = (JRadioButton)e.nextElement(); if (b.getModel() == group.getSelection()) { return b; } } return null; }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.