Using Java's Abstract Window Toolkit

From EDM2
Jump to: navigation, search

by Herman Rodriguez and Miguel Sang

JavaSoft's Abstract Window Toolkit (AWT) may be one of the most important elements in Java, since it provides the most visible component of any Java program. The AWT implementation in the Java Development Kit (JDK), version 1.02, provides the building blocks for basic Java graphical programs. This article gives an overview of the Java AWT in its present form, and also presents the current approach being taken by JavaSoft for future releases of the AWT and Java.

AWT Overview

A graphical user interface (GUI) window in Java is designed as a container that holds GUI elements. Since Java is object oriented, these GUI elements are referred to as GUI objects, but they're more commonly known as GUI controls or GUI components. The GUI components provided by the AWT include the basic GUI elements such as buttons and checkboxes. So, a simple Java window with a push button is composed of a kind of container with a Button component added. To organize these components within a container, the AWT provides the layout manager class interfaces. Layout managers are described later in this article. The Java GUI model is event driven, like most GUI windowing systems. So an event is the mechanism by which AWT elements communicate with each other.

Containers, Components and Events

The Component class is the superclass for most of the classes in the AWT. The Container class extends Component. This design allows a container to hold another container. A typical GUI window is composed of several containers within containers. Note that objects that inherit from the Container class may contain other components including Applet. The hierarchy and positioning of the GUI elements of a window in Java generally reflect the elements you would find in common graphics applications on OS/2, Windows and Motif. The AWT provides two classes that extend the Container class. These are:

  • Panel Class: Provides a generic container.
  • Window Class: Provides a generic window with no controls.

Other important subclasses are:

  • Frame: Extends Window. Provides a generic window with a title.
  • Dialog: Extends Window. Dialog Boxes are similar to window frames, but their use is intended to be short-lived, and are intended to be used as notification of warnings and other important events. Dialog boxes can be modal, thereby preventing input to any other window on the screen until the dialog has been completed. Since the dialog box is a panel, you can add components and set the layout as you can for any other panel. Also, remember to show the dialog using the show ( ) method, since the dialog is invisible when created. You also can hide the dialog, the same as hiding any other window, using the hide ( ) method.
  • FileDialog: Extends Dialog. Provides a window file dialog box.

Although not part of the AWT package, the Applet class also is a subclass of the Container class and thus it can contain any AWT component. The Applet class is provided in the java.applet package. Figure 1 illustrates the relationship of the above classes.

Figure 1. Major subclasses of Container with arrows pointing to the base class

The following basic subclasses of Component are provided by the AWT:

  • Button: Triggers an action event when pressed.
  • Canvas: Creates a drawing surface on which you can perform many operations, including painting images.
  • Checkbox: Use to select or deselect an option. A checkbox can be used in either exclusive or nonexclusive manner. If a checkbox is exclusive, it operates like a radio button. If the checkboxes are nonex- clusive, they are used to select one or more related options simultaneously.
  • Choice: A pop-up menu that enables selection of an item from a list of predefined choices.
  • Label: A text string to use as a label for other components.
  • List: A list of items.
  • Scrollbar: Provides the window scrollbars.
  • TextArea: An area for multi-line text.
  • TextComponent: Contains text that is editable.
  • Text Field: Allows for text entry into a field.

Layout Managers determine the position and other attributes (e.g. size) of the GUI components on the screen. Layout managers are implemented as class interfaces. Each class that extends the container class has a layout manager interface associated with it. Every subclass of Container has a default layout manager. The following layout managers are provided by the AWT:

  • FlowLayout: This is the most basic of all layouts, where components are to a panel's row, and are flowed to the next row when additional space is needed. The default behavior of the FlowLayout manager is to align all components in a row and centered in the panel. This is the default layout manager for classes that extend Panel.
  • BorderLayout: Adding components using this layout class allows the programmer to specify location using geographic directions north, south, east, west and center to place components on the border of the panel. This is the default layout manager for classes that extend Window.
  • GridLayout: This layout offers greater control on the placement of components within a panel. An area of the panel is portioned into a grid of rows and columns. As components are added, they are placed in each cell of the grid starting at the top row and placing additional components from left to right.
  • GridBagLayout: This is a variation of the GridLayout class that allows greater control over the placement of elements. It allows the components to be different sizes.
  • CardLayout: This layout class allows you to place components that do not need to be shown all at once, but one at a time. The components are arranged as individual cards. This layout is typically used to contain other panels. However, each panel in this layout may use a different layout manager.

A Simple Window

The AWT Window class provides you with the capability to create custom application windows. The Frame class allows you to create a complete window with a menubar. The Dialog provides a more limited form of a Window and is used for dialog boxes. Use the new Frame() constructor to create a frame without a title, or you can pass a String to the constructor to assign a title to your frame.

Since frames are containers, you can add other components to your frame as you would do for panels. For example:

  import java.applet.*;
  import java.awt.*;

  public class MyWindowApplet extends Applet
     public void init()
        add(new Button("1"));
        add(new Button("2"));
        add(new Button("3"));
        add(new Checkbox("Item 1"));
        add(new Checkbox("Item 2"));
        add(new Checkbox("Item 3"));
        add(new Label("This is a label"));

use the following HTML file to see the results:

   <head><title>A Java Applet</title>
   <applet code=MyWindowApplet.class width=400 height=200>

Notice how the GUI components are laid out by row, left to right and centered. This is the FlowLayout layout manager and it's the default for the Panel class (and all classes that extend it, including Applet).


An event object is used in the AWT to communicate that some asynchronous condition has occurred. For example, pressing a mouse button is an event. To process an event in your program, you need to override the applicable method to notify you when the condition of interest has occurred. For example, if you are interested in capturing mouse action, you need to override the mouseMove(), mouseDown(), and mouseUp() methods.

Events are passed to your event handler as event objects, where you can test the event's ID and process the specific event you are interested in by using variables in Event.
Figure 3. A Java application

The result produced by a GUI component is a special event called an action. An action event is passed to your class if you defined an action() method, allowing you to receive the notification of the event and process it as appropriate. Action events can be generated by a variety of sources, such as buttons when selected, checkboxes when selected, text fields when text is entered, etc.

Keyboard events are similarly generated when a key is pressed on the keyboard. When a key is pressed, a keyboard event can be captured by providing a keyDown() method. Likewise, when the key is released, a keyUp() method is called to notify you of that action. You can interrogate the particular key pressed by using the passed argument that contains the ASCII character value of that key. An example of an application handling events is shown in Figure 3.

Using AWT in Standalone Applications

You are not restricted to simply creating applets in order to use AWT. You can write standalone applications and still use all the AWT features. This is because you can create frames, which are general purpose mechanisms, from which you can create windows and panels. The following is an example of a standalone application with event handling.

In this example, we specify the FlowLayout layout manager to override the default for subclasses of Window (which is BorderLayout). The size of the window is specified using the resize() method. The window is invisible until you show the window using the show() method. In this example, the only event handled is the WINDOW_DESTROY event. It is received by the application when the user closes the window (by double-clicking on the top left control or pressing Alt-F4). Notice that the super keyword is used to pass the event to the superclass's handleEvent method before handling it ourselves.

The AWT 1.1

As of this writing, JavaSoft has released an enhanced AWT in Java version 1.1. The new improved AWT facilitates greater control of the graphical user interface and improves performance at the same time. Some of the features you can look for in AWT 1.1 are:

  • The ability to transfer data between applications using cut, copy and paste and clipboard support. Included in this new support is the ability to handle data of a variety of data formats (called "flavors" in AWT 1.1)
  • The ability to integrate color support with the desktop, to give a consistent appearance for Java and native applications and controls.
  • A new event model over and above the current one which is based solely on inheritance. This new model is called a "delegation model" and has as its goals:
    • Binary compatibility with the current AWT.
    • Greater separation between the bulk of the application and its GUI components.
    • Stronger compile time checks to reduce event handling errors.
    • Allowing a variety of models for event flow and propagation through the application.
    • Features to allow Java tool builders greater access and discovery of both events that a component generates and the events the component may observe.
    • New support for setting and resetting the clip area.
    • New support for simultaneous crop, scale and flip of an image.
    • Enhanced image scaling.
    • Enhanced animation capabilities with greater performance.
    • Enhanced PixelGrabber support for greater control of pixel data.
    • Support for mouseless operation, particularly important where keyboard operation is the preferred method of interaction.
    • Enhanced and new support for popup menus to increase code reuse.
    • Expanded support for scrolling. The current version only supports the scrollbar, yet left it to the programmer to perform the scroll function. In the new AWT, ScrollPane provides a container that implements automatic scrolling.
    • Printing AWT components using native print facilities.


The AWT package of Java classes provides the basic building blocks for building graphic applications that capitalize on the platform independence of Java. AWT allows programmers to manage user interaction using a graphical interface with unparalleled platform independence. However, fairly significant features, such as printing, are completely left out of the toolkit. The current architecture of the AWT also has created significant complexity in implementing certain features, such as scrolling. In addition, the mechanisms to accomplish significant functions, such as manipulating the clipboard, that are expected in most programs are not provided.

To solve these weaknesses, JavaSoft is making a series of improvements starting with the AWT in Java version 1.1. These enhancements are the first steps towards improving the AWT to make it a full-featured graphical user interface environment. At this moment it is clear that the advantages far outweigh the deficiencies.

Reprint Courtesy of International Business Machines Corporation, © International Business Machines Corporation