java.awt.CheckboxGroup example

The following program asks the customer how they're going to pay for their pizza, Visa, Mastercard, American Express, Discover, cash or check. Someone may want both anchovies and pineapple on their pizza, but they're unlikely to pay with both Visa and American Express.

Radio buttons in an applet

import java.applet.*;    
import java.awt.*;

public class PaymentMethod extends Applet {

  public void init() {
    this.add(new Label("How will you pay for your pizza?"));
    CheckboxGroup cbg = new CheckboxGroup();
    this.add(new Checkbox("Visa", cbg, false));
    this.add(new Checkbox("Mastercard", cbg, false));
    this.add(new Checkbox("American Express", cbg, false));
    this.add(new Checkbox("Discover", cbg, false));
    this.add(new Checkbox("Cash", cbg, true)); // the default
  }
  
}

There isn't any action in this simple example applet. If you need to add action as radio buttons are checked and unchecked, you do it just the same as for any other Checkbox.


Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified June 18, 1998