Applets are compiled Java programs
designed to be small, fast, and easily transferrable over network
resources. Applets are ideal for the Web because they leverage browser
features which decrease file size, increase download speed, and simplify
the programming. Applets, however, have limitations which full-blown
Java applications do not have. The limitations of applets can be
described as follows:
- Applets have limited file access
- Applets have limited network access
The Basics
The Life of an Applet
Applets have many different activities that correspond to various major events in the life of the applet.
Each activity has its own method so that when an event occurs, the browser (or other Java-capable tool) calls those
specific methods. Applets have four lifecycle methods: init(), start(), stop(), and
destroy(). These methods are called automatically, but you can override any of these methods to perform
specific tasks during execution.
- init() is the first method called by an applet once it has been loaded by the browser. It is called before
the applet begins execution, and can be overridden to perform any initialization tasks such as loading images or fonts,
establishing layout, or setting parameters.
- After an applet has been loaded by the browser and init() has been called, start() is automatically
called to begin the execution of the applet. To provide startup behavior for your applet, override the start()
method.
- stop() is called when the user moves off the page containing the applet. You implement this to stop any
operations you started in the start() method.
- destroy() is called when an applet has completely finished executing, and resources allocated by the applet
are returned to the system. Java takes care of garbage collection and memory allocation, so it is generally not necessary
to override destroy() unless you have specific resources that need to be released.
The Abstract Windowing Toolkit (AWT)
Java's AWT is a portable windowing library that contains a vast number of classes which cover all aspects of the visual
appearance of a window, including menus, toolbars, mouse operations, and more. Most of the basic AWT is subclassed from the
Component class. Because of this, components are the building blocks from which all programs utilizing AWT are built. AWT
components can be broken down conceptually into three major catagories (Taken from Java Unleashed):
- Interface components encompass all of the standard widgets associated with a windowing system. These widgets include:
buttons, canvases, checkboxes, labels, lists, scrollbars, text fields, and text areas.
- Containers encompass areas in which components can be placed, allowing several components to be grouped together to
form a more cohesive object for manipulation. Panel is the most commonly used container.
- Windows are a special case of the Component class, and are not used very often in applets. In applets, the browser
provides the main window and menu bar, so you don't need windows.
Layout managers in Java allow the user even greater control over the placement of components. The layout manager
determines how portions of the screen will be sectioned and how components within that panel will be placed. There are four
different layout managers available in Java. They are discussed in the table below.
| Layout Name | Function |
| BorderLayout | Layout according to compass points |
| GridLayout | Layout on a grid |
| GridBagLayout | Layout on a grid where elements can be different sizes |
| CardLayout | Layout that contains a series of "cards" which can be flipped through |
| FlowLayout | Layout that arranges components left to right |
So that is a basic description of Java's Abstract Windowing Toolkit. You will learn more about this as we look at applets and
observe how different components and layouts are implemented to create a user interface.
Threads
Just as multitasking operating systems can do more than one thing concurrently by running more than a single
process, a process can do the same by running more than a single thread. Each thread is a different
stream of control that can execute its instructions independently, allowing a multithreaded process to perform
numerous tasks concurrently. Thus using threads efficiently manages different parts of a program. By using threads in
Java, you can create an applet that runs in its own thread without interfering with any other part of the system.
The general rule of thumb for applets: If you have any bit of processing that is likely to continue for a
long time (such as an animation loop), put it in a thread.
There are four modifications you need to make to create an applet that uses threads (Taken from Java Unleashed):
- Change the signature of your applet class to include the words implement Runnable.
- Include an instance variable to hold this applet's thread.
- Modify your start() method to do nothing but spawn a thread and start it running.
- Create a run() method that contains the actual code that starts your applet running.
Handling User Input
Event Handling
The UI (User Interface) components defined in dataPanel need to handle user input. To intercept an action
by any UI component, we must define an action() method in our applet.
All basic UI components have different actions and arguments (taken from Teach Yourself Java In 21 Days by Laura
Lemay and Charles L. Perkins):
- Buttons create actions when they are selected, and a button's argument is the label of the button.
- Checkboxes, both exclusive and nonexclusive, generate actions when a box is checked. The argument
is always true.
- Choice Menus generate an action when a menu item is selected, and the argument is that item.
- Text fields create actions when the user presses Return inside that text field. Note that if the user
tabs to a different text field or uses the mouse to change the input focus, an action is NOT generated. Only
a Return triggers the action.
There are three necessary components to run an applet on the Internet:
Send comments and suggestions to jdecarolis@clarku.edu
Last updated 10 October 1997