Java: Get Selected JRadioButton From ButtonGroup
Posted by tech on
September 18, 2009
|
|
The easiest and best way to get which JRadioButton got selected is to place them in a ButtonGroup object. This way, a group of JRadioButtons can be isolated from other group(s) of JRadioButtons and can easily be accessed.
The method below does the trick.
1 2 3 4 5 6 7 8 9 | 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; } |







