I have a swing application that includes radio buttons on a form. I have the ButtonGroup, however, looking at the available methods, I can’t seem to get the name of the selected JRadioButton. Here’s what I can tell so far:
-
From ButtonGroup, I can perform a
getSelection()to return theButtonModel. From there, I can perform agetActionCommand, but that doesn’t seem to always work. I tried different tests and got unpredictable results. -
Also from
ButtonGroup, I can get an Enumeration fromgetElements(). However, then I would have to loop through each button just to check and see if it is the one selected.
Is there an easier way to find out which button has been selected? I’m programing this in Java 1.3.1 and Swing.
I would just loop through your
JRadioButtonsand callisSelected(). If you really want to go from theButtonGroupyou can only get to the models. You could match the models to the buttons, but then if you have access to the buttons, why not use them directly?