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

RadioButtons Help

Help is available for each task, or you can go straight to the RadioButtons source code to see a solution.

Task 1

Create an Applet to hold one instance of RadioButtons so you can test your component.

You need to create a class separate from your RadioButtons class. You can call it RadioButtonsTest. In this class, create an instance of RadioButtons and add it to the container.

import java.awt.*;
public class RadioButtonsTest extends java.applet.Applet {
    public void init() {
        RadioButtons test = new RadioButtons();
        add(test);
    }
}

Task 2

Create the class RadioButtons as a subclass of FormElement.

class RadioButtons extends FormElement {
    ...
}

Task 3

In the class RadioButtons, create a left-justified Label ("Java for: ") and two Checkbox objects ("C++ Programmers" and "Procedural Programmers") to the right, controlled by a CheckboxGroup.

class RadioButtons extends FormElement {
    protected CheckboxGroup cbg;
    protected String        label = "Java for: ";
    public RadioButtons() {
        setLayout(new FlowLayout());
        add(new Label(label));
        cbg = new CheckboxGroup();
        add(new Checkbox("C++ Programmers", cbg, true));
        add(new Checkbox("Procedural Programmers", cbg, false));
    }
    ...
}

Task 4

Implement the method getContents of FormElement. It should return the Label and the text of the selected course.

You need to ask the CheckboxGroup for the currently active Checkbox. Then, you can ask that Checkbox for its label.

public String getContents() {
    return label + "    " + cbg.getCurrent().getLabel();
}