Java Reference Index

  1. Introduction
  2. Data Types
  3. Arrays
  4. Operators
  5. Program Control
  6. Classes
  7. Objects
  8. Interfaces
  9. Containers
  10. Class Structure
  11. Error Handling

    Program Types

  12. Applications
  13. Applets

    How To

  14. Type Conversions


  15. java.lang.reflect
  16. java.applet
  17. java.awt
  18. java.beans
  20. java.lang
  21. java.math
  23. java.rm
  26. java.text
  27. java.util
  28. java.servlet


  29. Object Class


  30. Terms
  31. Credits


The main applet class that begins the running of the applet must be declared public.

public class MyApplet extends java.applet.Applet {
    // Code here

"Instead of a main() method, applets have methods that are called when specific things occur as the applet runs." The Applet class provides the below methods, but they may be overridden in your applet. The following methods provide the main functionality for applets to be created and destroyed.

  • init() - Initialization is done when the applet is loaded.
  • start() - This starts the applet running and may be done several times. This method must be overridden to start some work or required threads.
  • stop() - Stopping the applet execution. It happens when the user exits the page. This should be overridden if the start method was overridden.
  • destroy() - Destruction allows the applet to complete any required cleanup before being removed from memory.

Applets do not have constructors since the init method is used to establish the applet.

    Drawing methods
  • paint(Graphics g) - This is "called whenever the applet's window needs to be displayed or redisplayed". The browser passes the graphics object to the paint method.
  • Update

Putting an Applet on a Web Page

<TITLE>An Applet Example</TITLE>
<P>The Applet example is: <BR>
<APPLET CODE=“MyApplet.class” WIDTH=200 HEIGHT=50>
<PARAM NAME=font VALUE=“TimesRoman”>
<PARAM NAME=size VALUE=“16”>
You see this if your browser does not include Java support.

If the OBJECT tag is used rather than the APPLET tag, use CLASSID in place of CODE to specify the file name. A JAR (JAVA Archive) file is a package of the applet (or application) along with all required supporting class files. The "jar" command is used to create these JAR files from class and gif files.

Applet Attributes

  • CODE - The name of the applet main class file.
  • CODEBASE - Specifies a different folder for the browser to look for the applet file.
    • LEFT
    • RIGHT
    • TOP - Align with the topmost item in the line.
    • BOTTOM
    • MIDDLE
  • PARAM - Used to pass parameters to applets. Parameters expected but not passed are passed as a NULL. Your program needs to test for NULL parameters and set a default for any expected parameters. The "getParameter" function is used to get the passed parameters in the applet.
  • HSPACE - Space in pixels between the applet and the text around it.

Other Applet Information

Applets inherit event related functionality from the Component class. The Component class is one of the superclasses of the Applet class.

  • addMouseListener
  • addKeyListener

Must implement MouseListener interface with the "implements" statement and "addMouseListener(this);" in the init() method.

UI - User interface component is used to interface to the user. Methods to manage UI components:

  • add() - Add a component
  • remove()
  • setLayout() - Set the layout manager manager. Controls the size and position of applet components.