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
Is part of the java.applet package
Is an extension of the Panel class
Object
\
Component
\
Container
\
Panel
\
Applet
Must be the superclass of any applet that is to be embedded in a Web page or viewed by a Java applet viewer. The Applet class provides a standard interface between applets and their environment.
Is subclassed to define an applet. For example,
public class MyApplet extends Applet
begins the definition of a class named MyApplet that inherits all the features of the Object, Component, Container, Panel, and Applet classes. All that remains is to supply custom features that define application processing.
Has many methods. The most frequently used are:
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:
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.
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.
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.
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:
By importing the java.applet package, accessing the Applet class is made easier
The applet needs no WindowListener interface because it isn't a window
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).
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
Which of the following statements are true of applets? (choose two)
By default, an applet has a title bar
By default an applet has a BorderLayout
An applet does not require a main() method
If an applet contains a main() method, it will not compile
An applet inherits the features of the Component class
Which of the following methods may be run more than once during an applet's lifetime? (choose two)
stop()
init()
destroy
refresh()
start()
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.
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?
stop()
refresh()stop()
init()
start()stop()
destroy()
init()
start()stop()
start()stop()
destroy()
start()