• Post Reply Bookmark Topic Watch Topic
  • New Topic

Easiest way to determine the selected JRadioButton in a Button Group

 
Paul Hoffman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the easiest way to determine which button in a JRadioButton group was selected?

I found this code...is there an easier way?

// 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;
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've got all the buttons in an array, you can loop over them and ask if each one isSelected() -- that's a little bit simpler. But otherwise, surprisingly, no.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!