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

RegistrationForm Help

Help is available for most tasks, or you can go straight to the RegistrationForm source code to see a solution.

Task 1

Create a RegistrationForm applet that defines lifecycle method init and event convenience method action (leave them empty for now).

import java.awt.*;
public class RegistrationForm extends java.applet.Applet {
    public void init() { }
    public boolean action(Event event, Object arg) { }
}

Task 2

In method init, create a Panel (which you can name mainPanel) that contains the following objects:

  1. RadioButtons
  2. ContactInfoForm
  3. JobTitleList
  4. CommentBox

These are all derived from FormElement. You will want to set the layout manager for mainPanel so that it lays out the compound components per the expected behavior.

Assume that the required classes exist and then define references to them in RegistrationForm.

public class RegistrationForm extends java.applet.Applet {
    protected RadioButtons course;
    protected ContactInfoForm contact;
    protected JobTitleList job;
    protected CommentBox comment;
    ...
}

In the init method, you need to create a Panel (mainPanel variable) that contains all of these objects. What should the layout be for the new Panel? Unfortunately, the commonly used AWT layout managers do not isolate the specification of size and position. For example, centering a Component over another Component is straightforward using BorderLayout, but the size of the components would be stretched to the extent of the Container! You can often get around this by using FlowLayout and making sure that the Container has the right size so that the Components wrap correctly, thus, getting the layout you want. This is the approach you can take for this main panel.

public void init() {
    Panel mainPanel = new Panel();
    mainPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
    course = new RadioButtons();
    contact = new ContactInfoForm();
    job = new JobTitleList();
    comment = new CommentBox();
    mainPanel.add(course);
    mainPanel.add(contact);
    mainPanel.add(job);
    mainPanel.add(comment);
    ...
}

Task 3

In the method init, create a "Course Registration" Label with "Dialog" in an 18-point bold font. Create a "Submit" Button.

Creating a title and Submit Button for the applet is easy where the setFont method is used to change the Font type, weight, and size:

public void init() {
    Label title = new Label("Course Registration", Label.CENTER);
    title.setFont(new Font("Dialog", Font.BOLD, 18));
    submit = new Button("Submit");
    ...
}

The Submit Button needs to be defined as an instance variable so that the action method can refer to it also:

public class RegistrationForm extends java.applet.Applet {
    protected Button submit;
    ...
}

Task 4

In method init, add the "Course Registration" title, mainPanel, and Submit Button to RegistrationForm applet so that they are laid out according to the expected behavior.

The overall applet layout has the title at the top, the mainPanel in the center, and the Submit Button at the bottom. As mentioned earlier, centering objects is easy with BorderLayout or GridLayout, but this stretches the components; for example, the Button would end up being as wide as the applet--an unattractive option. There is little choice in this situation, but to use a layout manager other than those provided by the AWT (note at this point that AWT's GridBagLayout could handle this situation, but is extremely complicated and beyond the scope of this short course). You will use a simple layout manager called RatioLayout that specifies the position and size of components as ratios of the Container size; a ratio of "c" indicates that you would like to have the Component centered in that dimension.

public void init() {
    ...
    // after creating title, mainPanel, submit
    setLayout(new RatioLayout());
    add("c,0", title);  // center title at top
    // put mainPanel below title, span x, 80% in y
    add("0,.1;1,.8", mainPanel);
    add("c,.90", submit); // center button near bottom
    ...
}

Task 5

In the method action (which is called when the Submit Button is pushed), if the course selection, contact information, and job title are all nonempty, dump the contents to System.out. See FormElement to learn how the form elements can be queried.

First you must check to see that the Submit Button was pushed, which is easily done by comparing the target of the Event with the Button you created. Next, you must ask course, contact, and job if they are empty. If any are empty, report an error to the System.err stream. Otherwise, ask each object for its contents and print them to System.out. Both isEmpty and getContents are methods defined by FormElement.

If the Submit Button is pushed, return true indicating that you handled the event, otherwise return false. See Events for more information on events and event-chain flow.

Here is the complete action method:

public boolean action(Event event, Object arg) {
    if (event.target == submit) {
        // if any element is non-empty, then give error
        if ( course.isEmpty() || contact.isEmpty() ||
             job.isEmpty() ) {
            System.err.println("You must fill in all " +
                               "required fields before "+
                               "you can submit the form");
            return true;
        }
        // dump all contents
        System.out.println(course.getContents());
        System.out.println(contact.getContents());
        System.out.println(job.getContents());
        System.out.println(comment.getContents());
        return true;
    }
    return false;
}