THE AWT LIBRARY



Now let's look closer at developing an interactive window in a Web (html) document. Java has a built-in library for windowing called the AWT library. The statement import java.awt.*; makes the AWT library available to the program. The AWT abstract windowing toolkit contains many classes for building a GUI (Graphical User Interface) in Java and for writing graphical programs. In this section we will focus on the windowing aspect of the interface and leave the graphics aspects until a later section.

Java's Windowing Environment

An applet or other window is composed of objects that inherit from AWT class Component. Components include not only widgets, such as buttons, text fields, and labels, but also containers (objects that inherit from class Container) such as windows and applets. A Container object, which is itself a component, can include other components. The inheritance hierarchy for AWT classes and the Applet class is shown below. (Note that the Applet class is not an AWT class.)

                                  Object
                                    |
                ----------------------------------------------
                |                                            |
            Component                                   MenuComponent
                |                                            |
        --------------------------------*            --------------------
        |        |           |                       |                  |
    Container  Canvas   TextComponent             MenuItem           MenuBar
        |                    |                       |
    -----------           ------------           --------------
    |         |           |          |           |            |
  Panel    Window      TextArea  TextField      Menu  CheckBoxMenuItem
    |         |
    |       ---------
    |       |       |
 Applet   Dialog  Frame
            |
         FileDialog
                            *----------------------------------------------
                                  |         |       |        |     |      |
                              Scrollbar  Button  Checkbox  Label  List  Choice

A GUI component can be asked to draw itself by applying its paint() method. A component's paint() method can be applied at any time, but in practice paint() is applied 1) when the component is first made visible, 2) whenever the component changes its appearance, and 3) whenever an external action, such as moving or resizing a window, changes the component's area.

One problem with this procedure is that the whole component must be drawn every time the paint() method is invoked (unless undesirable coding is included to try to decide why the paint request was made). To allow more control, each component also has an update() method that might do some limited drawing on itself rather than repainting the whole component. The update() method is invoked by a repaint() request. The window manager decides when to process a repaint() request, so a component might not be redisplayed immediately after a repaint() request. There are inherited paint(), update(), and repaint() methods, so for each component it is only necessary to implement those methods for which the default (inherited) method is not satisfactory.

User interaction through window components is managed by the event handler. Actions such as clicking with the mouse or entering text into a text field invoke the event handler, which performs the necessary actions to process the event (including invoking a method that can perform any needed program actions).

If all of this seems confusing to you now, do not worry. These concepts will be illustrated in the next few examples. First, we begin with a simple expansion of the "Hi Mom" program that uses three widgets: two text fields and a button. Then we will implement a game called "Hilo". If you have some experience building GUIs you may wish to skip the "Hi Mom" example, or scan through it quickly, and go on to the Hilo example.