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

Solution

Download RegistrationForm source code.

HTML Interface to Applet


<APPLET CODEBASE="/applets/magelang/AWT-Training/classes/" CODE="RegistrationForm.class" WIDTH=495 HEIGHT=310 ALIGN=CENTER></APPLET>

Java Code


import java.awt.*;

/**
 * Simple fill-out form (w/verification) using custom
 * components and a custom layout manager for placement of
 * the overall applet.
 */
public class RegistrationForm extends java.applet.Applet {
    protected Button submit;
    protected RadioButtons course;
    protected ContactInfoForm contact;
    protected JobTitleList job;
    protected CommentBox comment;
    
    /**
     * Called once to initialize the Applet.
     * Create Components, lay them out, and perform any
     * initializations here.
     */
    public void init() {
        setFont(new Font("Dialog", Font.BOLD, 12));
        setBackground(Color.white);
        
        Label title = new Label("Course Registration");
        title.setFont(new Font("Dialog", Font.BOLD, 18));

        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);

        submit = new Button("Submit");

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

    /**
     * Respond to a "Submit" button press
     */
    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;
    }
}