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:

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.

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):
  1. 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.

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

  3. 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 NameFunction
BorderLayoutLayout according to compass points
GridLayoutLayout on a grid
GridBagLayoutLayout on a grid where elements can be different sizes
CardLayoutLayout that contains a series of "cards" which can be flipped through
FlowLayoutLayout 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):

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

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