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

Button Help

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

Task 1

Create an applet called TextFieldTest and add a new instance of the TextField class to the applet.

import java.awt.*;
public class TextFieldTest extends java.applet.Applet {
    public void init() {
        TextField text = new TextField();
        add(text);
    }
}

Task 2

Define method action such that when the user presses Return inside the text field, the enclosed text is sent to System.out.

Pressing Return inside the TextField will cause action to be called with a text argument inside the TextField. All you have to do is print it out and indicate that you handled the event by returning true.

public class TextFieldTest extends java.applet.Applet {
    ...
    public boolean action(Event e, Object text) {
        System.out.println("You typed '" + text + "'");
        return true;
    }
}