Download the JDK
Search the Tutorials
Hide the TOC
Trail: Deployment
Lesson: Applets
Section: Getting Started with Applets
Applets
Getting Started with Applets
Importing Classes and Packages for Applets
Defining an Applet Subclass
The Life Cycle of an Applet
Methods for Milestones
Methods for Drawing and Event Handling
Methods for Adding UI Components
What Applets Can and Can't Do
Taking Advantage of the Applet API
Finding and Loading Data Files
Displaying Short Status Strings
Displaying Documents in the Browser
Sending Messages to Other Applets
Playing Sounds
Defining and Using Applet Parameters
Deciding Which Parameters to Support
Writing the Code to Support Parameters
Giving Information about Parameters
Using the APPLET Tag
Practical Considerations When Writing Applets
Security Restrictions
Creating a User Interface
Creating a GUI
Displaying Diagnostics to the Standard Output and Error Streams
Getting System Properties
Threads in Applets
Threads in Applets: Examples
Working with a Server-Side Application
A Simple Network Client Applet
Using a Server to Work Around Security Restrictions
Finishing an Applet
Before You Ship that Applet
The Perfectly Finished Applet
Deploying Applets
General Deployment Considerations
Using the applet Tag
Using the object Tag
Using the embed Tag
Deploying Applets in a Mixed-Browser Environment
Solving Common Applet Problems
Questions and Exercises
Home Page > Deployment > Applets
« Previous • Trail • Next »
The Life Cycle of an Applet

> Here is the `Simple` applet.

> The following is the source code for the `Simple`. The `Simple` applet displays a descriptive string whenever it encounters a major milestone in its life, such as when the user first visits the page the applet's on. The pages that follow use the `Simple` applet and build upon it to illustrate concepts that are common to many applets. If you find yourself baffled by the Java source code, you might want to go to Learning the Java Language to learn about the language.

> >     /*
> >      * Java(TM) SE 6 Version
> >      */

> >     import java.applet.Applet;
> >     import java.awt.Graphics;

> >     //No need to extend JApplet, since we don't add any components;
> >     //we just paint.
> >     public class Simple extends Applet {

> >         StringBuffer buffer;

> >         public void init() {
> >             buffer = new StringBuffer();
> >             addItem("initializing... ");
> >         }

> >         public void start() {
> >             addItem("starting... ");
> >         }

> >         public void stop() {
> >             addItem("stopping... ");
> >         }

> >         public void destroy() {
> >             addItem("preparing for unloading...");
> >         }

> >         private void addItem(String newWord) {
> >             System.out.println(newWord);
> >             buffer.append(newWord);
> >             repaint();
> >         }

> >         public void paint(Graphics g) {
> >     	//Draw a Rectangle around the applet's display area.
> >             g.drawRect(0, 0,
> >     		   getWidth() - 1,
> >     		   getHeight() - 1);

> >     	//Draw the current string inside the rectangle.
> >             g.drawString(buffer.toString(), 5, 15);
> >         }
> >     }

> > Note: In this example, we extend the `Applet` class, not the Swing `JApplet` class, as we do not need to add Swing components to this applet.

### Loading the Applet

> You should see "initializing... starting..." above, as the result of the applet being loaded. When an applet is loaded, here's what happens:
> - An instance of the applet's controlling class (an `Applet` subclass)is created.
> - The applet initializes itself.
> - The applet starts running.

### Leaving and Returning to the Applet's Page

> When the user leaves the page — for example, to go to another page — the browser stops the applet. When the user returns to the page, the browser starts the applet.

> > Browser note: Some browsers reload the applet when you return to its page. In at least one browser, a bug exists where an applet can initialize itself more than once without being reloaded.

### Reloading the Applet

> Some browsers let the user reload applets, which consists of unloading the applet and then loading it again. Before an applet is unloaded, it's given the chance to stop itself and then to perform a final cleanup, so that the applet can release any resources it holds. After that, the applet is unloaded and then loaded again, as described in Loading the Applet, above.

> > Try this: If your browser or other applet viewer lets you easily reload applets, reload the applet. Look at the standard output to see what happens when you reload the applet. (See Displaying Short Status Strings for information about the standard output.) You should see "stopping..." and "preparing for unloading..." when the applet is unloaded. You can't see this in the applet GUI because the applet is unloaded before the text can be displayed. When the applet is reloaded, you should see "initializing..." and "starting...", just like when you loaded the applet for the first time.

### Quitting the Browser

> When the user quits the browser (or whatever application is displaying the applet), the applet has the chance to stop itself and do final cleanup before the browser exits.

### Summary

> An applet can react to major events in the following ways:
> - It can initialize itself.
> - It can start running.
> - It can stop running.
> - It can perform a final cleanup, in preparation for being unloaded.
> The next page describes the four applet methods that correspond to these four types of reactions.

« Previous • Trail • Next »
Previous page: Defining an Applet Subclass
Next page: Methods for Milestones