A good answer might be:


Example Program

Radio buttons generate action events just as do push buttons. Use setActionCommand(String) with them and register a listener using addActionListener(ActionEvent).

Usually radio buttons are placed in (1) a panel, to control how they are displayed, and (2) a button group, to control which buttons may be active simultaneously. A button group is an object which must be constructed. Radio buttons are then added to it.

Let us create an application that calculates a person's ideal weight (in pounds) given their gender and height (in inches). The actual calculation is given in the programming exercises. For now let us look at the graphical interface. The GUI uses radio buttons. The following code shows how a button group is constructed and how buttons are added to it.

Radio buttons generate action events, just as push buttons do. A complete application requires an action listener to respond to radio button pushes. Do this as with push buttons: use setActionCommand() to assign a command string to each button, and use getActionCommand() in the action listener to determine which button was pushed. Our example program leaves this to the programming exercises.

public class IdealWeight extends JFrame
  . . . .
  public IdealWeight()  
    genderM = new JRadioButton("Male", true );
    genderF = new JRadioButton("Female", false );
    genderGroup = new ButtonGroup();
    genderGroup.add( genderM );
    genderGroup.add( genderF );
   . . . . .


How many button groups are used in this GUI?