1  /* create some radio buttons */
  2  
  3  import java.awt.*;
  4  
  5  public class CheckboxGroupTest extends java.applet.Applet {
  6  
  7    private Label label;
  8    private int n = 6;  // number of radio buttons
  9    private Checkbox[] radioButton = new Checkbox[n];
 10    private CheckboxGroup radioButtons = new CheckboxGroup();
 11    
 12    public void init() {
 13      setBackground(Color.white);
 14  
 15      String[] colorLabel = new String[n];
 16      colorLabel[0] = "Red";    colorLabel[1] = "Blue";
 17      colorLabel[2] = "Yellow"; colorLabel[3] = "Green";
 18      colorLabel[4] = "Orange"; colorLabel[5] = "Purple";
 19      
 20      Panel radioButtonPanel = new Panel();
 21      for ( int i = 0; i < n; i++ ) {
 22        radioButton[i] = new Checkbox(colorLabel[i], radioButtons, false);
 23        radioButton[i].setBackground(Color.white);
 24        radioButtonPanel.add(radioButton[i]);
 25      }
 26      radioButtons.setCurrent(radioButton[0]);
 27      
 28      setLayout(new GridLayout(2,1,5,5));
 29      add(radioButtonPanel);
 30      label = new Label("", Label.LEFT);
 31      label.setBackground(Color.white);
 32      add(label);
 33    }
 34  
 35    public boolean action(Event event, Object arg) {
 36      if ( event.target instanceof Checkbox ) {
 37        updateLabel();
 38        return true;
 39      }
 40      return super.action(event, arg);
 41    }
 42    
 43    public void updateLabel() {
 44      int i = 0;
 45      while ( !radioButton[i].getState() && i < n ) i++;
 46      String str = "   You chose color ";
 47      str += radioButton[i].getLabel() + "!";
 48      label.setText(str);
 49    }
 50    
 51  }