Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

The Java Lesson 34: An introduction to applets and the Applet class

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala

Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

Java Lesson 34 by Jon Huhtala: An introduction to applets and the Applet class


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





  • 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:




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


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


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


Requests that this applet be resized


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


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);
message.setFont(new Font("Serif", Font.ITALIC, 24));

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


  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()

  2. stop()

  3. stop()

  4. stop()

  5. stop()

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy