An introduction to applets and the Applet class

 

Overview

An applet is a small program that is intended not to be run on its own, but rather to be embedded inside or launched from within another application. The most common use of applets are the small programs launched from Web pages. To view some sample applets, click here.

Applets are graphical and event driven but, for security reasons, may never access the file system of the platform on which they are run. Because they inherit from classes within the java.awt package, much of what you need to know about creating your own custom applets has already been covered!

 

The Applet class

Object

\

Component

\

Container

\

Panel

\

Applet

Method

Usage

destroy()

Automatically called by the browser or applet viewer to inform this applet that it is being destroyed and that it should release any resources it holds

getParameter()

Returns the value of a named parameter within the HTML tag for this applet

init()

Automatically called by the browser or applet viewer to inform this applet that it has been loaded into the system

resize()

Requests that this applet be resized

start()

Automatically called by the browser or applet viewer to inform this applet that it should start its execution

stop()

Automatically called by the browser or applet viewer to inform this applet that it should stop its execution

Other methods exist for retrieving image files, retrieving and playing audio clips, and more. Consult the Java API for details.

 

The life cycle of a Web page applet

The processing of all Web page applets is as follows:

  1. The applet's init() method is automatically called when the page containing the applet is loaded by the browser or applet viewer. Its purpose is to perform applet initialization and it is called only once during the life of the applet. For a simple applet, this is the only required method. An applet does not need a main() method. If one exists, it is ignored.

  2. The applet's start() method is automatically called after init() and every time the page is revisited. Its purpose is to resume suspended applet processing when the user returns to the page after surfing.

  3. The applet's stop() method is automatically called when the user surfs to another page or when the browser is being shut down. Its purpose is to suspend applet processing.

  4. The applet's destroy() method is automatically called after stop() when the browser or applet viewer is being shut down. Its purpose is to release all applet resources and it is called only once during the life of the applet.

 

A sample applet

The following statements define a small applet that displays a message when the user clicks a button. The button may then be re-clicked to clear the message:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class App extends Applet implements ActionListener {

  Button b = new Button("Show message");
  TextField message = new TextField("", 15);

  public void init() {
    resize(300, 100);
    setBackground(Color.lightGray);
    b.addActionListener(this);
    add(b);
    message.setFont(new Font("Serif", Font.ITALIC, 24));
    message.setForeground(Color.red);
    message.setEditable(false);
    add(message);
  }

  public void actionPerformed(ActionEvent e) {
    if (message.getText().length() == 0) {
      message.setText("Hello world!");
      b.setLabel("Clear message");
    }
    else {
      message.setText("");
      b.setLabel("Show message");
    }
  }
}

Notes:

  1. By importing the java.applet package, accessing the Applet class is made easier

  2. The applet needs no WindowListener interface because it isn't a window

  3. Processing begins with the init() method. It immediately resizes the window to override any size specified by the applet's HTML tag (to be covered in the next lesson). Everything else about the applet is virtually the same as you would see in an equivalent windows program (with the exception of window event handling which isn't needed).

  4. NOTE: The procedure for compiling a Java applet is identical to the procedure for compiling a Java program. To test the applet's bytecode, however, you must launch the applet viewer program. This is done by entering the command:    appletviewer App.html

 

Lab exercise for Ferris students

E-mail your answers to this assignment no later than the due date listed in the class schedule.

 

Review questions

  1. Which of the following statements are true of applets?  (choose two)

  1. By default, an applet has a title bar

  1. By default an applet has a BorderLayout

  2. An applet does not require a main() method

  3. If an applet contains a main() method, it will not compile

  4. An applet inherits the features of the Component class

  1. Which of the following methods may be run more than once during an applet's lifetime?  (choose two)

  1. stop()

  1. init()

  2. destroy

  3. refresh()

  4. start()

  1. Assume that an applet named Checkoff is to be defined that can be referenced from any other class and only requires the ability to respond to Checkbox events. Code its class header.

  1. Which one of the following represents the sequence in which applet methods will automatically be called when a user surfs to a different Web page and then returns to the page containing the applet?

  1. stop()
    refresh()

  2. stop()
    init()
    start()

  3. stop()
    destroy()
    init()
    start()

  4. stop()
    start()

  5. stop()
    destroy()
    start()