The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Writing Applets
Lesson: Overview of Applets

## The Life Cycle of an Applet

> Below is the `Simple` applet.

> > You can't run applets. Here's what you'd see if you could:

> > Note: Because some old browsers don't support 1.1, the above applet is a 1.0 version (here is the 1.0 code; here's the 1.1 code). To run the 1.1 version of the applet, go to `example-1dot1/Simple.html`. For more information about running applets, refer to About Our Examples.

### 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 applet has the option of stopping itself. When the user returns to the page, the applet can start itself again. The same sequence occurs when the user iconifies and then reopens the window that contains the applet. (Other terms used instead of iconify are minaturize, minimize, and close.)

> > Try this: Leave and then return to this page. You'll see "stopping..." added to the applet output above, as the applet is given the chance to stop itself. You'll also see "starting...", when the applet is told to start itself again. Next, iconify this window, and then open it again. Many window systems provide a button in the title bar that lets you iconify the window. You should see "stopping...." and then "starting...." added to the applet output.

> > 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.

| Start of Tutorial > Start of Trail > Start of Lesson | Search