The Web Design Group

APPLET - Java Applet

Syntax <APPLET>...</APPLET>
Attribute Specifications
  • CODE=CDATA (class file)
  • CODEBASE=URI (base URI for class files)
  • WIDTH=Length (applet width)
  • HEIGHT=Length (applet height)
  • ARCHIVE=CDATA (archive files)
  • OBJECT=CDATA (serialized applet)
  • NAME=CDATA (name for inter-applet communication)
  • ALT=Text (alternate text)
  • ALIGN=[ top | middle | bottom | left | rightá] (applet alignment)
  • HSPACE=Pixels (horizontal gutter)
  • VSPACE=Pixels (vertical gutter)
  • common attributes
Contents PARAM elements followed by block-level elements and/or inline elements
Contained in Inline elements, block-level elements except PRE

The APPLET element is used to embed Java applets. It has been deprecated in HTMLá4.0 in favor of the more generalized OBJECT element. However, since the few browsers that support OBJECT do so with significant bugs, APPLET is currently a more reliable method of embedding Java applets.

APPLET's CODE attribute specifies the name of the class file that contains the compiled Applet subclass. The value is relative to the URI specified in the CODEBASE attribute, or to the HTML document's base URI if the CODEBASE attribute is not given.

The required WIDTH and HEIGHT attributes define the dimensions of the applet. The value may be given in pixels or as a percentage of the parent element's width or height.

The ALT attribute can be used to give alternate text for browsers that recognize the APPLET element but do not support Java or do not have Java enabled. Authors can also give alternate content between the start and end tags of the APPLET element--a better method than using the ALT attribute since it allows authors to include HTML markup in the alternate content and also works with pre-HTMLá3.2 browsers that do not support APPLET.

An APPLET may contain PARAM elements to define applet-specific parameters. PARAM elements should be specified before any other content of the APPLET element. In the following example, a decorative Java applet takes two parameters. The APPLET contains an animated GIF as an alternative for non-Java browsers.

<APPLET CODE="Animate.class" WIDTH=100 HEIGHT=100>
<PARAM NAME=img1 VALUE="/images/1.jpg">
<PARAM NAME=img2 VALUE="/images/2.jpg">
<IMG SRC="animation.gif" ALT="" WIDTH=100 HEIGHT=100>
</APPLET>

The ARCHIVE attribute can specify a comma-separated list of archived files (either absolute URIs or URIs relative to the CODEBASE), allowing the browser to download many files with a single connection and hence decreasing the total download time. The standard archive format for Java files is JAR. JAR files can be created with the jar tool included with the Java Development Kit 1.1 and up.

Note that some browsers do not support the ARCHIVE attribute, so all necessary files should be available unarchived as well. Other browsers only support a single URI as the ARCHIVE value.

The OBJECT attribute specifies a serialized (saved) representation of an applet. The CODE attribute should not be used if and only if the OBJECT attribute is specified. When the applet is deserialized, its init() method is not invoked, but its start() method is. Sun recommends restraint in using this poorly supported feature.

The ALIGN attribute specifies the alignment of the applet. The values top, middle, and bottom specify the applet's position with respect to surrounding content on its left and right.

ALIGN=middle aligns the center of the applet with the current baseline. To center the applet horizontally on the page, place the applet in a centered block, e.g.,

<P ALIGN=center><APPLET CODE="Game.class" WIDTH=300 HEIGHT=100></APPLET></P>

The other ALIGN values, left and right, specify a floating applet; the applet is placed at the left or right margin and content flows around it. To place content below the applet, use <BR CLEAR=left|right|all> as appropriate.

The vertical-align and float properties of Cascading Style Sheets provide more flexible methods of aligning applets.

The HSPACE and VSPACE attributes allow an author to suggest horizontal gutters and vertical gutters, respectively, around the applet. The value must be in pixels and applies to both sides of the applet. Style sheets provide more flexibility in specifying the space around applets.

More Information