Copyright Chris Johnson, 1998.

Human Computer Interface Design Using Java

Chris
The APPLET Tag
by

Chris Johnson

Based on notes from Mary Campione and Kathy Walrath's Java Tutorial at java.sun.com.



Applets provide the primary mechanism of introducing interaction into html pages using AWT. There are certain rules about the tags that you use to include an Applet within an HTML page. Here is a brief reminder. In the following, Bold font indicates something you should type in as shown. You must substitute a value for words in italics. Square brackets ([ and ]) indicate optional contents.

< APPLET
    [CODEBASE = codebaseURL]
    CODE = appletFile 
    [ALT = alternateText]
    [NAME = appletInstanceName]
    WIDTH = pixels
    HEIGHT = pixels 
    [ALIGN = alignment] 
    [VSPACE = pixels]
    [HSPACE = pixels]
>
[< PARAM NAME = appletParameter1 VALUE = value >]
[< PARAM NAME = appletParameter2 VALUE = value >]
. . .
[alternateHTML]
</APPLET>
CODEBASE = codebaseURL
This optional attribute specifies the base URL of the applet -- the directory or folder that contains the applet's code. If this attribute is not specified, then the document's URL is used.

CODE = appletFile
This required attribute gives the name of the file that contains the applet's compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute.

ALT = alternateText
This optional attribute specifies any text that should be displayed if the browser understands the APPLET tag but can't run Java applets.

NAME = appletInstanceName
This optional attribute specifies a name for the applet instance, which makes it possible for applets on the same page to find (and communicate with) each other.

WIDTH = pixels
HEIGHT = pixels
These required attributes give the initial width and height (in pixels) of the applet display area, not counting any windows or dialogs that the applet brings up.

ALIGN = alignment
This required attribute specifies the alignment of the applet. The possible values of this attribute are the same (and have the same effects) as those for the IMG tag: left, right, top, texttop, middle, absmiddle, baseline, bottom, absbottom.

VSPACE = pixels
HSPACE = pixels
These optional attributes specify the number of pixels above and below the applet (VSPACE) and on each side of the applet (HSPACE). They're treated the same way as the IMG tag's VSPACE and HSPACE attributes.

< PARAM NAME = appletParameter1 VALUE = value >
<PARAM> tags are the only way to specify applet-specific parameters. Applets read user-specified values for parameters with the getParameter() method. See Defining and Using Applet Parameters for information about the getParameter() method.

alternateHTML
If the HTML page containing this <APPLET> tag is viewed by a browser that doesn't understand the <APPLET> tag, then the browser will ignore the <APPLET> and <PARAM> tags, instead interpreting any other HTML code between the <APPLET> and </APPLET> tags. Java-compatible browsers ignore this extra HTML code.

In the online version of this tutorial, we use alternate HTML to show a snapshot of the applet running, with text explaining what the applet does. Other possibilities for this area are a link to a page that is more useful for the Java-ignorant browser, or text that taunts the user for not having a Java-compatible browser.