I have two JRadioButtons that will be used to sort objects in a JList in various ways. They are both members of the same ButtonGroup. However, I'd like to listen to both buttons with one actionlistener and then iterate through them to see which one was selected. What is the most effecient way of doing such?
Asked
Active
Viewed 888 times
2 Answers
1
One way of doing it is using ButtonGroup's getSelection, which returns the ButtonModel of the JRadioButton that is selected. Your ActionListener should be able to distinguish which one is selected by examining this.

Klarth
- 2,035
- 14
- 26
-
but be sure to check if the returned ButtonModel is null (if no JRadioButton has yet been selected) first before getting its actionCommand – Hovercraft Full Of Eels Apr 02 '11 at 05:29
1
The ActionListener tells you which button was clicked in the ActionEvent:
JRadioButton button = (JRadioButton)event.getSource();

camickr
- 321,443
- 19
- 166
- 288
-
-
Yeah, sorry, I got mixed up and didn't really think that through. Again, thanks for the input! – Austin H Apr 02 '11 at 05:20