Explain Abstract Window Toolkit (AWT), AWT CLASSES WITH EXAMPLES, How AWT Works In AppletIn This Topic & Blog Having Any Query Then Post your Comment and Suggestion Below
The AWT contains
numerous classes and methods that allow you to create and manage windows. It is also
the foundation upon which Swing is built. The AWT is quite large and a full description would
easily fill an entire book. Therefore, it is not possible to describe in detail every AWT class,
method, or instance variable. However, this and the following two chapters explain the basic
techniques needed to use the AWT effectively when creating your own applets or stand-alone
GUI-based applications. From there, you will be able to explore other parts of the AWT on your
own. You will also be ready to move on to Swing.
In this chapter, you will learn how to create and manage windows, manage fonts, output
text, and utilize graphics. Chapter 24 describes the various controls, such as scroll bars and
push buttons, supported by the AWT. It also explains further aspects of Java’s event handling
mechanism. Chapter 25 examines the AWT’s imaging subsystem and animation.
Although a common use of the AWT is in applets, it is also used to create stand-alone
windows that run in a GUI environment, such as Windows. For the sake of convenience,
most of the examples in this chapter are contained in applets. To run them, you need to use
an applet viewer or a Java-compatible web browser. A few examples will demonstrate the
creation of stand-alone, windowed programs.
One other point before beginning. Today, most Java programs employ user interfaces
based on Swing. Because Swing provides richer implementations than does the AWT of
some common GUI controls, such as buttons, lists, and check boxes, it is easy to jump to
the conclusion that the AWT is no longer important, that it has been superseded by Swing.
This assumption is, however, quite wrong. As mentioned, Swing is built on top of the AWT.
Thus, many aspects of the AWT are also aspects of Swing. Furthermore, many AWT classes
are used either directly or indirectly by Swing. Finally, for some types of small programs
(especially small applets) that make only minimal use of a GUI, using the AWT rather than
Swing still makes sense. Therefore, even though most interfaces today will be based on
Swing, a solid knowledge of the AWT is still required. Simply put, you can’t be a great Java
programmer without knowing the AWT.
If you have not yet read Chapter 22, please do so now. It provides an overview of event
handling, which is used by many of the examples in this chapter.
The AWT classes are contained in the
package. It is one of Java’s largest packages.
Fortunately, because it is logically organized in a top-down, hierarchical fashion, it is easier
to understand and use than you might at first believe. Table 23-1 lists some of the many
Encapsulates AWT events.
Dispatches events to multiple listeners.
The border layout manager. Border layouts use five components:
North, South, East, West, and Center.
Creates a push button control.
A blank, semantics-free window.
The card layout manager. Card layouts emulate index cards.
Only the one on top is showing.
Creates a check box control.
Creates a group of check box controls.
Creates an on/off menu item.
Creates a pop-up list.
Manages colors in a portable, platform-independent fashion.
An abstract superclass for various AWT components.
A subclass of
that can hold other components.
Encapsulates a bitmapped cursor.
Creates a dialog window.
Specifies the dimensions of an object. The width is stored in
and the height is stored in
Creates a window from which a file can be selected.
The flow layout manager. Flow layout positions components left
to right, top to bottom.
Encapsulates a type font.
Encapsulates various information related to a font. This information
helps you display text in a window.
Creates a standard window that has a title bar, resize corners, and
a menu bar.
A Sampling of AWT Classes
W orking with W indows, Graphics, and T ext
Encapsulates the graphics context. This context is used by the
various output methods to display output in a window.
Describes a graphics device such as a screen or printer.
Describes the collection of available
Defines various constraints relating to the
The grid bag layout manager. Grid bag layout displays components
subject to the constraints specified by
The grid layout manager. Grid layout displays components in
a two-dimensional grid.
Encapsulates graphical images.
Encapsulates the borders of a container.
Creates a label that displays a string.
Creates a list from which the user can choose. Similar to the
standard Windows list box.
Manages media objects.
Creates a pull-down menu.
Creates a menu bar.
An abstract class implemented by various menu classes.
Creates a menu item.
Encapsulates a keyboard shortcut for a menu item.
The simplest concrete subclass of
Encapsulates a Cartesian coordinate pair, stored in
Encapsulates a polygon.
Encapsulates a pop-up menu.
An abstract class that represents a print job.
Encapsulates a rectangle.
Supports automated testing of AWT-based applications.
Creates a scroll bar control.
A container that provides horizontal and/or vertical scroll bars
for another component.
Contains the colors of GUI widgets such as windows, scroll bars,
text, and others.
Creates a multiline edit control.
A superclass for
Creates a single-line edit control.
Abstract class implemented by the AWT.
Creates a window with no frame, no menu bar, and no title.
A Sampling of AWT Classes (continued)
Although the basic structure of the AWT has been the same since Java 1.0, some of the
original methods were deprecated and replaced by new ones. For backward-compatibility,
Java still supports all the original 1.0 methods. However, because these methods are not for
use with new code, this book does not describe them.
The AWT defines windows according to a class hierarchy that adds functionality and
specificity with each level. The two most common windows are those derived from
, which is used by applets, and those derived from
, which creates a standard
application window. Much of the functionality of these windows is derived from their
parent classes. Thus, a description of the class hierarchies relating to these two classes is
fundamental to their understanding. Figure 23-1 shows the class hierarchy for
. Let’s look at each of these classes now.
At the top of the AWT hierarchy is the
is an abstract class that
encapsulates all of the attributes of a visual component. All user interface elements that are
displayed on the screen and that interact with the user are subclasses of
over a hundred public methods that are responsible for managing events, such as mouse and
keyboard input, positioning and sizing the window, and repainting. (You already used many
of these methods when you created applets in Chapters 21 and 22.) A
responsible for remembering the current foreground and background colors and the currently
selected text font.
class is a subclass of
. It has additional methods that allow other
objects to be nested within it. Other
objects can be stored inside of a
(since they are themselves instances of
This makes for a multileveled
containment system. A container is responsible for laying out (that is, positioning) any
W orking with W indows, Graphics, and T ext
components that it contains. It does this through the use of various layout managers, which
you will learn about in Chapter 24.
class is a concrete subclass of
It doesn’t add any new methods; it simply
may be thought of as a recursively nestable, concrete screen
is the superclass for
. When screen output is directed to an applet,
it is drawn on the surface of a
object. In essence, a
is a window that does not
contain a title bar, menu bar, or border. This is why you don’t see these items when an applet
is run inside a browser. When you run an applet using an applet viewer, the applet viewer
provides the title and border.
Other components can be added to a
object by its
method (inherited from
Once these components have been added, you can position and resize them
manually using the
setLocation( ) setSize( ) setPreferredSize( )
class creates a top-level window. A top-level window is not contained within any
other object; it sits directly on the desktop. Generally, you won’t create
directly. Instead, you will use a subclass of
, described next.
encapsulates what is commonly thought of as a “window.” It is a subclass of
and has a title bar, menu bar, borders, and resizing corners. If you create a
within an applet, it will contain a warning message, such as “Java Applet Window,” to the
user that an applet window has been created. This message warns users that the window
they see was started by an applet and not by software running on their computer. (An applet
that could masquerade as a host-based application could be used to obtain passwords and
other sensitive information without the user’s knowledge.) When a
window is created
by a stand-alone application rather than an applet, a normal window is created.
Although it is not part of the hierarchy for applet or frame windows, there is one other type
of window that you will find valuable:
encapsulates a blank window upon
which you can draw. You will see an example of
later in this book.
Working with Frame Windows
After the applet, the type of window you will most often create is derived from
use it to create child windows within applets, and top-level or child windows for stand-alone
applications. As mentioned, it creates a standard-style window.
Here are two of
Frame(String title )
The first form creates a standard window that does not contain a title. The second form creates
a window with the title specified by title. Notice that you cannot specify the dimensions of
the window. Instead, you must set the size of the window after it has been created.
There are several key methods you will use when working with
are examined here.
Setting the Window’s Dimensions
method is used to set the dimensions of the window. Its signature is shown here:
void setSize(int newWidth , int newHeight )
void setSize(Dimension newSize )
The new size of the window is specified by newWidth and newHeight , or by the
fields of the
object passed in newSize . The dimensions are specified in
terms of pixels.
method is used to obtain the current size of a window. Its signature is
Dimension getSize( )
This method returns the current size of the window contained within the
fields of a
Hiding and Showing a Window
After a frame window has been created, it will not be visible until you call
Its signature is shown here:
void setVisible(boolean visibleFlag )
The component is visible if the argument to this method is
. Otherwise, it is hidden.
Setting a Window’s Title
You can change the title in a frame window using
, which has this general form:
void setTitle(String newTitle )
Here, newTitle is the new title for the window.
Closing a Frame Window
When using a frame window, your program must remove that window from the screen when
it is closed, by calling
To intercept a window-close event, you must implement
method of the
you must remove the window from the screen. The example in the next section illustrates