// Purpose. JRadioButton demo import javax.swing.*; import java.awt.event.*; public class JRadioButtonDemo { public static JPanel createRadioButtons( String[] choices ) { JPanel rPanel = new JPanel(); Box radios = Box.createVerticalBox(); rPanel.add( radios ); ItemListener rbil = new ItemListener() { public void itemStateChanged( ItemEvent e ) { System.out.println( ((AbstractButton)e.getItem()).getText() + " is " + ((e.getStateChange() == ItemEvent.SELECTED) ? "on" : "off") ); } }; JRadioButton rb; ButtonGroup bg = new ButtonGroup(); for (int i=0; i < choices.length; i++) { rb = new JRadioButton( choices[i] ); radios.add( rb ); rb.addItemListener( rbil ); bg.add( rb ); } return rPanel; } public static void main( String[] args ) { JFrame frame = new JFrame( "JRadioButton demo" ); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); String[] choices = { "Alpha","Bravo","Charlie","Delta","Echo","Foxtrot" }; frame.getContentPane().add( createRadioButtons( choices ) ); frame.setSize( 300, 240 ); frame.setVisible( true ); } } // Delta is on // Delta is off // Bravo is on // Bravo is off // Foxtrot is on // Foxtrot is on