An Applet with Checkboxes


The applet's real estate is the area within the border

Source

AnAppletWithCheckboxes.java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithCheckboxes extends Applet implements ItemListener {
    public void init() {
        setLayout(new GridLayout(1, 0));

        CheckboxGroup dinnerType = new CheckboxGroup();
        standard = new Checkbox("standard", dinnerType, false);
        standard.addItemListener(this);
        deluxe = new Checkbox("deluxe", dinnerType, true);
        deluxe.addItemListener(this);

        CheckboxGroup appetizers = new CheckboxGroup();
        crepe = new Checkbox("crepe", appetizers, false);
        crepe.addItemListener(this);
        quiche = new Checkbox("quiche", appetizers, false);
        quiche.addItemListener(this);
        dumpling = new Checkbox("dumpling", appetizers, false);
        dumpling.addItemListener(this);

        CheckboxGroup soupOrSalad = new CheckboxGroup();

        soup = new Checkbox("Soup", soupOrSalad, false);
        soup.addItemListener(this);

        CheckboxGroup soups = new CheckboxGroup();
        cream = new Checkbox("cream", soups, false);
        cream.addItemListener(this);
        broth = new Checkbox("broth", soups, false);
        broth.addItemListener(this);
        gumbo = new Checkbox("gumbo", soups, false);
        gumbo.addItemListener(this);

        salad = new Checkbox("Salad", soupOrSalad, false);
        salad.addItemListener(this);

        CheckboxGroup salads = new CheckboxGroup();
        tossed = new Checkbox("tossed", salads, false);
        tossed.addItemListener(this);
        caesar = new Checkbox("caesar", salads, false);
        caesar.addItemListener(this);
        croutons = new Checkbox("croutons");
        croutons.addItemListener(this);
        lite = new Checkbox("lite dressing");
        lite.addItemListener(this);


        CheckboxGroup entrees = new CheckboxGroup();
        chicken = new Checkbox("chicken", entrees, false);
        chicken.addItemListener(this);
        beef = new Checkbox("beef", entrees, false);
        beef.addItemListener(this);
        lamb = new Checkbox("lamb", entrees, false);
        lamb.addItemListener(this);
        fish = new Checkbox("fish", entrees, false);
        fish.addItemListener(this);

        CheckboxGroup deserts = new CheckboxGroup();
        pie = new Checkbox("pie", deserts, false);
        pie.addItemListener(this);
        fruit = new Checkbox("fruit", deserts, false);
        fruit.addItemListener(this);
        sherbet = new Checkbox("sherbet", deserts, false);
        sherbet.addItemListener(this);


        setLayout(new GridLayout(0, 1));

        Panel p = new Panel();
        add(p);
        p.add(standard);
        p.add(deluxe);

        appetizerPanel = new Panel();
        add(appetizerPanel);
        appetizerPanel.add(new Label("Appetizer"));
        appetizerPanel.add(crepe);
        appetizerPanel.add(quiche);
        appetizerPanel.add(dumpling);

        soupPanel = new Panel();
        add(soupPanel);
        soupPanel.add(soup);
        soupPanel.add(cream);
        soupPanel.add(broth);
        soupPanel.add(gumbo);

        saladPanel = new Panel();
        add(saladPanel);
        saladPanel.add(salad);
        saladPanel.add(tossed);
        saladPanel.add(caesar);
        saladPanel.add(croutons);
        saladPanel.add(lite);


        entreePanel = new Panel();
        add(entreePanel);
        entreePanel.add(new Label("Entree"));
        entreePanel.add(chicken);
        entreePanel.add(beef);
        entreePanel.add(lamb);
        entreePanel.add(fish);

        desertPanel = new Panel();
        add(desertPanel);
        desertPanel.add(new Label("Desert"));
        desertPanel.add(pie);
        desertPanel.add(fruit);
        desertPanel.add(sherbet);
	}

    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == standard || e.getSource() == deluxe)
            handleDinnerType((Checkbox)e.getSource());
        else if (e.getSource() == soup || e.getSource() == salad)
            handleSoupSaladChoice((Checkbox)e.getSource());
    }

    void handleDinnerType(Checkbox selectedType) {
        boolean enabled;
        if (selectedType == standard)
            enabled = false;
        else
            enabled = true;
        pie.setEnabled(enabled);
        fruit.setEnabled(enabled);
        sherbet.setEnabled(enabled);
        cream.setEnabled(enabled);
        broth.setEnabled(enabled);
        gumbo.setEnabled(enabled);
        tossed.setEnabled(enabled);
        caesar.setEnabled(enabled);
        croutons.setEnabled(enabled);
        lite.setEnabled(enabled);

    }

    void handleSoupSaladChoice(Checkbox selectedCourse) {
        boolean soupEnabled, saladEnabled;
        if (selectedCourse == soup) {
            soupEnabled = true;
            saladEnabled = false;
        }
        else {
            soupEnabled = false;
            saladEnabled = true;
        }
        cream.setEnabled(soupEnabled);
        broth.setEnabled(soupEnabled);
        gumbo.setEnabled(soupEnabled);

        tossed.setEnabled(saladEnabled);
        caesar.setEnabled(saladEnabled);
        croutons.setEnabled(saladEnabled);
        lite.setEnabled(saladEnabled);
    }

    Panel
        appetizerPanel, soupPanel, saladPanel, entreePanel, desertPanel;

    Checkbox
        standard, deluxe,
        soup, salad,
        crepe, quiche, dumpling,
        cream, broth, gumbo,
        tossed, caesar,
        croutons, lite,
        chicken, beef, lamb, fish,
        pie, fruit, sherbet;
}

Notes

About this Tutorial

This tutorial is somewhat different than the previous one-- it is more than a simple bare-boned demonstration of a new AWT GUI component. While it does introduce a new component-- the Checkbox, it also illustrates how the programmer can introduce more complex logic into the event handlers in order to enhance the behavior of the interface.

The Checkbox Component

The Checkbox component allows the user to check off one or more choices from a fixed set of choices. Checkbox objects may either exist in isolation from each other (as in the croutons/lite dressing) choices, or they may be created as belonging to a CheckboxGroup object, in which case they are mutually exclusive (only one box in the group may be selected)-- in which case they are often called radio buttons.

This Applet's Behavior

Play with the applet a bit to get a feel for the behavior.

The Logic and Structure of the Applet

Some aspects of the applet's behavior comes from the behavior of the Checkbox component and that of the CheckboxGroup class: Other parts of the behavior were programmed into the event handler (the itemStateChanged class) and the supporting methods (handleDinnerType, handleSoupSaladChoice): Notice how we use the setEnabled method to control access to the various choices.

Things to Do

API work Playing With the Applet

Make the following changes to AnAppletWithCheckboxes