In this chapter you will learn:
What the Abstract Windows Toolkit is
How to create AWT Containers
How to place AWT components in Containers
How to use Layout Managers to control the look and feel of your GUI
Creating Graphical User Interfaces using Java is fairly straightforward. This simplicity is hampered somewhat by the fact that there are two different, but related, sets of graphics classes: the Abstract Windows Toolkit (AWT) and the Java Foundation Classes (JFC), which are collectively known as Swing.
When Java 1.0 was released circa 1995, it shipped with a set of classes called the Abstract Windows Toolkit 1.0. Later, this toolkit was upgraded to AWT 1.1, which improved it considerably. This version is the one that we will discuss in this chapter. The JFC, or Swing Toolkit, will be the topic of Chapter 16, "Swing."
The AWT is built on the native graphics system of each platform that it is ported to. For example, when you create a button on the Windows platform, AWT creates a peer control in the native Windows graphics library. Whenever your users communicate with your button, it passes the information on to the peer to do the heavy lifting.
Several problems are inherent in this approach. The first problem is that these components are considered heavyweight because they cause a potentially large object to be created under the covers. This object might have considerable functionality that is never used by the AWT button, but the JVM cannot instantiate half of the peer. It must all be instantiated.
The second problem is that every graphics subsystem differs in the objects that it supports and also in the features each object provides. This forced the AWT development team to use a "least common denominator" approach, where only the object types that all platforms support could be used. This guaranteed that the AWT would be mired permanently in mediocrity.
Regretting what they had done, the AWT development team abandoned the peer approach and reimplemented the graphical objects in native Java code. They renamed it the Java Foundation Classes (Swing). AWT was not replaced, however, for reasons of backward compatibility.
Java application developers quickly noticed the superiority of the Swing classes and moved quickly to use them. Applet developers, however, were hampered by the unwillingness of browser vendors to support the new libraries.
The Java development team responded by providing a plug-in for both Internet Explorer and Netscape Navigator. This plug-in replaces the default version 1.1 JVM with Java 2 code. Even though the newer versions of the browser support Swing via the plug-in, not all users update their browsers regularly.
Finally, we have the problem of users turning off all Java functionality in their browsers. They do this based on the occurrence of some security problems associated with applets years ago. Power-user types who write in magazines made the sweeping recommendation that Java always be disabled.
The resulting situation is that you have three categories of browsers that you have to consider when writing applets: the browser with Java disabled, the browser with Java 1.1 enabled, and the browser with Java 2 enabled using a plug-in. Many Web-page developers avoid this situation by avoiding Java Applets and implementing all their functionality by using HTML only. This is a very limiting practice given the weakness of HTML as a programming language.
Another group programs applets using Swing and requires that the users install the Java plug-in. This only works where the programmer has considerable control over the browser on his users' desktops. For the other 90% of the Web applications that are trying to reach all Internet users, the programmer cannot control what the user is running. For this reason, most Java programmers still write their applets using AWT.
In this chapter, we will cover the Abstract Windows Toolkit (AWT) only. First, you will learn how to create and run AWT container classes. Following that, you will learn about the different types of controls that are available in the AWT. Finally, you will study the management of page layout using the Layout Manager classes.