AWT Components

We discussed Swing components in Chapter 10. In this section, we introduce the older AWT components, which you can use in Internet-ready applets. [1] The program in Figure 114 illus-trates the graphical UI (GUI) components that the AWT provides. With the exception of menus, every AWT component is implemented with a subclass of the AWT Component [1] class.

[1] By "Internet ready," we mean that the applets will run in most Web browsers without the Java Plug-in installed. These applets are based on the 1.0 and 1.1 JDKs (an earlier version of the Java 2 Platform).


Figure 114. Snapshot of the an applet that uses all the AWT GUI components.



Remember that programs should not use "heavyweight" AWT components alongside Swing components. Heavyweight components include all the ready-to-use AWT components and all components that inherit from the AWT Canvas and Panel classes. When Swing components (and all other "lightweight" components) overlap with heavyweight components, the heavyweight component is always painted on top.


AWT Basic Controls

The Button, Checkbox, Choice, List, MenuItem, and TextField classes provide basic controls. These are the most common ways that users give instructions to programs. When a user activates one of these controlsby clicking a button or by pressing Return in a text field, for exampleit posts an action event. See the documentation for each class for examples of handling action events.

Other Ways of Getting User Input

When the basic controls aren't appropriate, you can sometimes use the Scrollbar and the TextArea classes to get user input. The Scrollbar class is used for both slider and scroll bar functionality. Scroll bars are automatically included in lists and text areas (as shown in the example program) and in ScrollPane.

The TextArea class provides an area to display or to allow editing of several lines of text. As you can see from the example program in Figure 114, text areas automatically include scrollbars.

Creating Custom Components

The Canvas class lets you write custom components. With your Canvas subclass, you can draw custom graphics to the screenin a paint program, image processor, or game, for exampleand implement any kind of event handling.


A Label object displays an unselectable line of text.


The AWT provides three types of containers, all implemented as subclasses of the Container [1] class, which is a Component subclass. The Window subclassesDialog, FileDialog, and Frameprovide windows to contain components. A Panel groups components within an area of an existing window. A ScrollPane is similar to a panel but has a more specialized purpose: to display a potentially large component in a limited amount of space, generally using scroll bars to control which part of the component is displayed.


Frames create normal, full-fledged windows, as opposed to the windows that Dialogs create, which are dependent on Frames and can be modal. When you select the File dialog... item in the menu, the program creates a FileDialog object, which is a Dialog that can be either an Open or a Save dialog.

Browser Note

Some browsers might not implement the FileDialog class if they never allow applets to read or to write files on the local file system. Instead of seeing a file dialog, you'll see an error message in the standard output or error stream. See the section Displaying Diagnostics to the Standard Output and Standard Error Streams (page 446) for information about applets' standard output.

Figure 115 shows a picture of the FileDialog window that the Applet Viewer brings up.

Figure 115. A window from the FileDialog applet.


The example program uses a Panel to group the label and the text area, another Panel to group them with a canvas, and a third Panel to group the text field, button, check box, and pop-up list of choices. All these Panels are grouped by a Frame object, which presents the window they're displayed in. The Frame also holds a menu and a list.

Other AWT Classes

In addition to components, the AWT contains a variety of classes related to drawing and event handling. This section discusses the AWT classes that are in the java.awt and java.awt.event packages. The AWT contains three other packagesjava.awt.datatransfer, java.awt.image, and java.awt.peerthat most programs don't have to use. The classes and the interfaces in those packages are discussed as needed elsewhere in this appendix.

As you learned in the previous section, components are grouped into containers. What the previous section didn't tell you is that each container uses a layout manager to control the on-screen size and position of the components it contains. The java.awt package supplies several layout manager classes. You learned all about layout managers in the section Layout Management (page 375) in Chapter 10.

The java.awt package supplies several classes to represent sizes and shapes. One is the Dimension class, which specifies the size of a rectangular area. Another is the Insets class, which is usually used to specify how much padding should exist between the outside edges of a container and the container's display area. Shape classes include Point, Rectangle, and Polygon.

The Color class is useful for representing and manipulating colors. It defines constants for commonly used colors, for example, Although it generally uses colors in RGB (red-green-blue) format, it also understands HSB (hue-saturation-brightness) format.

The Image class provides a way to represent image data. Applets can get Image objects for GIF and JPEG images by using the Applet getImage methods. Programs that are not applets get images by using a different helper class: Toolkit. The Toolkit class provides a platform-independent interface to the platform-dependent implementation of the AWT. Although that sounds impressive, most programs don't deal with Toolkit objects directly, except to get images. Images are loaded asynchronouslyyou can have a valid Image object even if the image data hasn't been loaded yet or doesn't exist. Using a MediaTracker object, you can keep track of the status of the image loading. MediaTracker currently works only with images, but it might eventually work with other media types, such as sounds.

To control the look of the text your program draws, use Font and FontMetrics objects. The Font class lets you get basic information about fonts and create objects representing various fonts. With a FontMetrics object, you can get detailed information about the size characteristics of a particular font. You can set the font a component uses by using the Component and Graphics setFont methods.

Finally, the Graphics class and various types from the java.awt.event package are crucial to the AWT drawing and event-handling system. A Graphics object represents a drawing contextwithout a Graphics object, no program can draw itself to the screen. The java.awt.event package defines classes such as MouseEvent, which represents user input made with a mouse or a similar device.

Getting Started

Object-Oriented Programming Concepts

Language Basics

Object Basics and Simple Data Objects

Classes and Inheritance

Interfaces and Packages

Handling Errors Using Exceptions

Threads: Doing Two or More Tasks at Once

I/O: Reading and Writing

User Interfaces That Swing

Appendix A. Common Problems and Their Solutions

Appendix B. Internet-Ready Applets

Appendix C. Collections

Appendix D. Deprecated Thread Methods

Appendix E. Reference

The Java Tutorial(c) A Short Course on the Basics
The Java Tutorial: A Short Course on the Basics, 4th Edition
ISBN: 0321334205
EAN: 2147483647
Year: 2002
Pages: 125 © 2008-2020.
If you may any questions please contact us: