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

ContactInfoForm Help

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

Task 1

Create an applet called ContactInfoFormTest that creates and displays an instance of ContactInfoForm.

import java.applet.*;
import java.awt.*;
public class ContactInfoFormTest extends Applet {
        public void init() {
                add(new ContactInfoForm());
        }
}

Task 2

Create a class named ContactInfoForm as a subclass of FormElement.

class ContactInfoForm extends FormElement {
    ...
}

Task 3

In the constructor, create and track references to a LabeledTextField (with argument "Name"), PhoneTextField, EMailTextField, and LabeledChoice (with argument "Payment Method").

The subelements will be added to an array so you can walk through them easily. The array should be an instance variable so that the other methods can see it:

class ContactInfoForm extends FormElement {
    int n=4; // how many fields in the form?
    FormElement[] fields = new FormElement[n];
    ...
}

The constructor can then create the subelements:

    public ContactInfoForm() {
        int i=0;
        fields[i++] = new LabeledTextField("Name");
        fields[i++] = new PhoneTextField();
        fields[i++] = new EMailTextField();
        fields[i++] = new LabeledChoice("Payment Method");
    }

Task 4

Add the components in such a way that they are arranged with one on top of the other in the order specified above.

You want the elements arranged in a single column with the right edges lined up or, more precisely, you want the LabeledTextField objects stretched so that their right edges line up. A GridLayout layout manager should be used because it arranges columns nicely and because it stretches the contained components. Augment the constructor to be:

    public ContactInfoForm() {
        // use a grid with padding of 10 and 10 (wid,height)
        setLayout(new GridLayout(0,1,10,10));
        int i=0;
        fields[i++] = new LabeledTextField("Name");
        fields[i++] = new PhoneTextField();
        fields[i++] = new EMailTextField();
        fields[i++] = new LabeledChoice("Payment Method");
        for (i=0; i<n; i++) {  // add the elements
            add(fields[i]);
        }
    }

Task 5

Define method public Dimension preferredSize so that it returns a width of 300 pixels and a height of 120 (30 pixels for each field).

Because the overall RegistrationForm applet will be indirectly resizing this element via the RatioLayout layout manager, you need to specify the preferred size of this element. To do this, override preferredSize:

    public Dimension preferredSize() {
        return new Dimension(300,n*30);
    }

Task 6

Define method getContents such that it returns a newline-separated list of the contents of the subelements.

All you have to do is walk through the list of elements and pack them together into one String, making sure that there is a newline between each element's content.

    public String getContents() {
        String contents="";
        for (int i=0; i<n; i++) {
            contents += fields[i].getContents();
            // no newline on the last contents
            if ( i<n-1 ) contents += "\n";
        }
        return contents;
    }

Task 7

Define method isEmpty such that it returns true if any of the subelements are empty else return false.

Walk the list of elements just as you did in getContents:

    public boolean isEmpty() {
        for (int i=0; i<n; i++) {
            if ( fields[i].isEmpty() ) return true;
        }
        return false;
    }

Task 8

Define the method verify so that it asks each subelement to verify itself.

Verify each subelement:

    public void verify() {
        for (int i=0; i<n; i++) {
            fields[i].verify();
        }
    }