![]() |
|||
![]() ![]() |
![]() |
|
![]() |
SwingSwing components allow for efficient graphical user interface (GUI) development. Swing components are a collection of lightweight visual components. They contain replacements for the basic heavyweight AWT components as well as complex user-interface components such as trees and tables. Swing components contain a pluggable look-and-feel, abbreviated as PL&F or plaf. This allows the same application to run with a native look-and-feel on different platforms. For example, when running on a Windows platform, the application will look like an application written specifically for the Windows-based computer. The same application, when run on an Apple Macintosh computer, looks like it was written specifically for the Macintosh. When the application is run on a UNIX workstation, it looks like a UNIX application, and so on for any platform to which the JVM has been ported. Another use of the PL&F is to have an application behave the same on any platform on which it is executed. The JFC contains operating systemneutral look-and-feels that allow an application to present the same look-and-feel independent of the operating system where it executes. This capability can reduce the learning curve for users of applications running in a heterogeneous computer environment. As a developer, you are not limited to the PL&F shipped with the JFC. You can develop your own look-and-feel that can be used by any JFC application. An organization may want to develop its own to give applications a consistent look-and-feel for use on all its hardware platforms. Unlike their AWT equivalents, Swing components do not contain peers. Swing components are 100% pure Java and were designed to allow for mixing AWT heavyweight and Swing lightweight components in an application. However, in practice this has proven problematic. Whenever possible you should use all Swing components and components derived from them. The major difference between lightweight and heavyweight components is that a lightweight component can have transparent pixels while a heavyweight component is always opaque. By taking advantage of transparent pixels, a lightweight component can appear to be non-rectangular, while a heavyweight component must always be rectangular. A mouse event occurring in a lightweight component falls through to its parent component, while a mouse event in a heavyweight component does not propagate through to its parent component. When a lightweight component overlaps a heavyweight component, the heavyweight component is always drawn on top of the lightweight component, regardless of the relative z-order of the two components. The Swing components are JavaBean compliant. This allows the components to be used easily in a Bean-aware application building program. The Swing release contains BeanInfo classes for the Swing UI (user interface) components to aid in using Swing in builder applications. With all these great new features provided by the JFC, it may be asked if the AWT is required at all. The answer is yes, it is. This is because Swing is an extension for the AWT, not a replacement. The root of the majority of the Swing hierarchy is the JComponent class. This class is an extension of the AWT Container class. Thus the necessity of the AWT classes, even when using Swing components. This is demonstrated in Figure 1.1. This figure shows that Swing is built on the base AWT classes, but not on the AWT UI component classes. For example, the AWT button or label classes are not extended.
When there are equivalent classes in each toolkit for a particular UI component, the Swing version should be used. For example, the AWT provides an implementation of a button in the Button class. Swing also provides an implementation for a button in the JButton class. The JButton class should be used in JFC applications. This allows the PL&F to be changed and the button to be drawn by using the current PL&F. The JFC contains many complex components, such as a tree and table, that simply are not available in the AWT. Swing components comprise a large percentage of the JFC release and are the major focus of this book. However they are not the only innovations contained in the JFC. The JFC contains the Java 2D classes. These classes provide an API for advanced two-dimensional graphic rendering in Java: many different paint styles, APIs for defining complex shapes, coordinate transformations, and more. The Drag and Drop API provides data transfer between Java and native applications as well as within a single Java application. The Accessibility APIs are provided to aid people with disabilities while interacting with JFC components. Unlike Swing, however, these portions of the JFC are not 100% pure Java implementations. The AWT components are to be enhanced to support these new features in the 1.2 release of the JDK.
|
![]() |
|