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 FormElement source code.

HTML Interface to Applet


<APPLET CODEBASE="/applets/magelang/AWT-Training/classes/" CODE="FormElement.class" WIDTH=300 HEIGHT=150 ALIGN=CENTER></APPLET>

Java Code


import java.awt.Panel;
import java.awt.Event;

/**
 * Abstract class to describe the functionality needed for
 * every field of a form.  A form contains spaces for user
 * input, therefore we want to be able to query whether there
 * is any contents.  We also we want the field to be able
 * to print itself.
 */
public abstract class FormElement extends Panel {
    /**
     * Returns a String containing text description of this
     * field and an appropriate label.
     * @return String text in this field
     */
    public abstract String getContents();

    /**
     * Query whether the field has valid contents
     * @return boolean true if empty else false
     */
    public boolean isEmpty() {
    	return false;
    }
    
    /** Verify the contents of the element.  Empty elements
     *  are always ok (until they hit submit anyway). 
     */
    public void verify() {}
    
    public boolean handleEvent(Event event) {
        switch (event.id) {
            // if they click mouse outside of element or activate it
            // we verify the element.
            case Event.LOST_FOCUS:
            case Event.ACTION_EVENT:
                if ( !isEmpty() ) verify();
                return true;
            default:
                return super.handleEvent(event);
        }
    }

}