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

Checkbox Help

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

Task 1

Create an applet called CheckboxTest. Create and add a new instance of a class you will create called MyCheckbox.

import java.awt.*;
public class CheckboxTest extends java.applet.Applet {
    public CheckboxTest() {
        MyCheckbox m = new MyCheckbox();
        add(m);
    }
}

Task 2

Create call MyCheckbox as a subclass of Checkbox. Make its constructor (which has no arguments) call the constructor of Checkbox with the label "Always Prompt."

class MyCheckbox extends Checkbox {
    public MyCheckbox() {
        super("Always Prompt");
    }
}

Task 3

Define method action in MyCheckbox such that when the user clicks on the MyCheckbox the state is printed to System.out.

When the user clicks on a Checkbox object, the method action is called on that Component with an argument of the new state (a Boolean object as opposed to the primitive type boolean).

class MyCheckbox extends Checkbox {
    ...
    public boolean action(Event e, Object state) {
        System.out.println("State is " + (Boolean) state);
        return true;
    }
}