Adding a Radio Button Group to a Panel
public RadioButtonPanel() {
redButton = new JRadioButton(“Red”, true) ; // Initially selected
blueButton = new JRadioButton(“Blue”, false) ; // Initially unselected
greenButton = new JRadioButton(“Green”, false) ; // . . . . . .
add(redButton) ; // Add all three buttons
add(blueButton) ; // to the panel (for display)
redButton.addActionListener(this) ;
blueButton.addActionListener(this) ;
greenButton.addActionListener(this) ;
ButtonGroup group = new ButtonGroup() ; // A group of buttons
group.add(redButton) ; // Add all three buttons to
group.add(blueButton) ; // the button group (for control)
setBackground(Color.red) ;