Web server setup - Chapter 12 Graphical User Interface Components: Part 1
Friday, January 18th, 2008Chapter 12 Graphical User Interface Components: Part 1 649 12.2 Swing Overview The classes that create the GUI components of Fig. 12.2 are part of the Swing GUI components from package javax.swing. These GUI components became standard in Java with the release of the Java 2 platform version 1.2. Most Swing components (as they are commonly called) are written, manipulated and displayed completely in Java (so-called pure Java components). The original GUI components from the Abstract Windowing Toolkit package java.awt (also called the AWT) are tied directly to the local platform s graphical user interface capabilities. When a Java program with an AWT GUI executes on different Java platforms, the program s GUI components display differently on each platform. Consider a program that displays an object of type Button (package java.awt). On a computer running the Microsoft Windows operating system, the Button will have the same look and feel as the buttons in other Windows applications. Similarly, on a computer running the Apple Macintosh operating system, the Button will have the same look and feel as the buttons in other Macintosh applications. In addition to the differences in appearance, sometimes the manner in which a user interacts with a particular AWT component differs between platforms. Together, the appearance and how the user interacts with the program are known as that program s look and feel. The Swing components allow the programmer to specify a uniform look and feel across all platforms. In addition, Swing enables programs to provide a custom look and feel for each platform or even to change the look and feel while the program is running. For example, a program could enable users to choose their preferred look and feel. Look-and-Feel Observation 12.2 Swing components are written in Java, so they provide a greater level of portability and flexibility than the original Java GUI components from package java.awt. Swing components are often referred to as lightweight components they are written completely in Java so they are not weighed down by the complex GUI capabilities of the platform on which they are used. AWT components (many of which parallel the Swing components) that are tied to the local platform are correspondingly called heavyweight components they rely on the local platform s windowing system to determine their functionality and their look and feel. Each heavyweight component has a peer (from package java.awt.peer) that is responsible for the interactions between the component and the local platform that display and manipulate the component. Several Swing components are still heavyweight components. In particular, subclasses of java.awt.Window (such as JFrame used in several previous chapters) that display windows on the screen and subclasses of java.applet.Applet (such as JApplet) still require direct interaction with the local windowing system. As such, heavyweight Swing GUI components are less flexible than many of the lightweight components we will demonstrate. Portability Tip 12.1 The look of a GUI defined with heavyweight GUI components from package java.awtmay vary across platforms. Heavyweight components tie into the local platform GUI, which varies from platform to platform. Figure 12.3 shows an inheritance hierarchy of the classes that define attributes and behaviors that are common to most Swing components. Each class is displayed with its Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01