Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information

Java Cup Logo

JDC Home Page


Working Applet
Help and Hints
Source Code
Table of Contents
Online Training
shadowSearchFAQFeedback

Solution

Download LabeledChoiceTest source code.

HTML Interface to Applet


<APPLET CODEBASE="/applets/magelang/AWT-Training/classes/" CODE="LabeledChoiceTest.class" WIDTH=300 HEIGHT=40 ALIGN=CENTER></APPLET>

Java Code


import java.awt.*;

public class LabeledChoiceTest extends java.applet.Applet {

    public void init() {
        LabeledChoice test = new LabeledChoice("Test");
        add(test);
    }
}

/**
 * Component containing both a Label and a Choice
 */
class LabeledChoice extends FormElement {
    protected Choice   choice;
    protected String   label = "Payment Method";

    public LabeledChoice(String label) {
        this.label = label;
        setLayout(new BorderLayout(5,0));

        choice = new Choice();
        choice.addItem("American Express");
        choice.addItem("Master Card");
        choice.addItem("Visa");
        choice.addItem("Cash");
        choice.addItem("Check");

        add("West", new Label(label, Label.LEFT));
        add("Center", choice);
    }

    /**
     * Return a text representation of this field.
     * This method is required by the FormElement interface.
     * @return String the selected text
     */
    public String getContents() {
        return label + "    " + choice.getSelectedItem();
    }
}