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

EMailTextField Help

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

Task 1

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

You need to create a class separate from your LabeledTextField class. You can call it LabeledTextFieldTest. In this class, create an instance of LabeledTextField and add it to the container. Here, as you did with LabeledTextField, use a GridLayout to manage the container so that the EMailTextField will be scaled to fit the window rather than appearing at its preferredSize.

import java.awt.*;

public class EMailTextFieldTest extends java.applet.Applet {

    public void init() {
        setLayout(new GridLayout(1,1));
        EMailTextField test = new EMailTextField();
        add(test);
    }
}

Task 2

Create class EMailTextField as a subclass of LabeledTextField. Make the constructor set the label to "E-Mail Address".

class EMailTextField extends LabeledTextField {

    public EMailTextField() {
        super("E-Mail Address");
    }
    ...
}

Task 3

Override the method verify of FormElement so that an error is printed if the text does not end with .com, .gov, .edu, .mil, .org, or .net. Also print an error if there is no @ symbol or it is the first character (there is no userid).

public void verify() {
    String address = text.getText().toUpperCase();
    if (!address.endsWith(".COM") &&
        !address.endsWith(".EDU") &&
        !address.endsWith(".GOV") &&
        !address.endsWith(".MIL") &&
        !address.endsWith(".ORG") &&
        !address.endsWith(".NET") ) {
        System.err.println("Invalid internet domain:  " +
                           "Please enter again.");
        text.setText("");
    }

    // check to ensure @ exists and is not first
    if (address.indexOf("@") <= 0) {
        System.err.println("Invalid user name:  " +
                           "Please enter again.");
        text.setText("");
    }
}