Overview of Swing Components

Though it is possible to perform input and output using the JOptionPane dialogs presented in Section 11.2, most GUI applications require more elaborate, customized user interfaces. The remainder of this chapter discusses many GUI components that enable application developers to create robust GUIs. Figure 11.4 lists several Swing GUI components from package javax.swing that are used to build Java GUIs. Most Swing components are pure Java componentsthey are written, manipulated and displayed completely in Java. They are part of the Java Foundation Classes (JFC)Java's libraries for cross-platform GUI development. Visit java.sun.com/products/jfc for more information on JFC.

Figure 11.4. Some basic GUI components.




Displays uneditable text or icons.


Enables user to enter data from the keyboard. Can also be used to display editable or uneditable text.


Triggers an event when clicked with the mouse.


Specifies an option that can be selected or not selected.


Provides a drop-down list of items from which the user can make a selection by clicking an item or possibly by typing into the box.


Provides a list of items from which the user can make a selection by clicking on any item in the list. Multiple elements can be selected.


Provides an area in which components can be placed and organized. Can also be used as a drawing area for graphics.

Swing vs. AWT

There are actually two sets of GUI components in Java. Before Swing was introduced in J2SE 1.2, Java GUIs were built with components from the Abstract Window Toolkit (AWT) in package java.awt. When a Java application with an AWT GUI executes on different Java platforms, the application's GUI components display differently on each platform. Consider an application 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 appearance as the buttons in other Windows applications. Similarly, on a computer running the Apple Mac OS X operating system, the Button will have the same look and feel as the buttons in other Macintosh applications. Sometimes, the manner in which a user can interact with a particular AWT component differs between platforms.

Together, the appearance and the way in which the user interacts with the application are known as that application's look-and-feel. Swing GUI components allow you to specify a uniform look-and-feel for your application across all platforms or to use each platform's custom look-and-feel. An application can even change the look-and-feel during execution to enable users to choose their own preferred look-and-feel.

Portability Tip 11.1

Swing components are implemented in Java, so they are more portable and flexible than the original Java GUI components from package java.awt, which were based on the GUI components of the underlying platform. For this reason, Swing GUI components are generally preferred.


Lightweight vs. Heavyweight GUI Components

Most Swing components are not tied to actual GUI components supported by the underlying platform on which an application executes. Such GUI components are known as lightweight components. AWT components (many of which parallel the Swing components), are tied to the local platform and are called heavyweight components, because they rely on the local platform's windowing system to determine their functionality and their look-and-feel.

Several Swing components are heavyweight components. Like AWT components, heavyweight Swing GUI components require direct interaction with the local windowing system, which may restrict their appearance and functionality. As you will learn, lightweight components provide you with more control over their appearance and functionality.

Look-and-Feel Observation 11.4

The look and feel of a GUI defined with heavyweight GUI components from package java.awt may vary across platforms. Because heavyweight components are tied to the local-platform GUI, the look and feel varies from platform to platform.


Superclasses of Swing's Lightweight GUI Components

The UML class diagram of Fig. 11.5 shows an inheritance hierarchy containing classes from which lightweight Swing components inherit their common attributes and behaviors. As discussed in Chapter 9, class Object is the superclass of the Java class hierarchy.

Figure 11.5. Common superclasses of many of the Swing components.

(This item is displayed on page 517 in the print version)

Software Engineering Observation 11.1

Study the attributes and behaviors of the classes in the class hierarchy of Fig. 11.5. These classes declare the features that are common to most Swing components.

Class Component (package java.awt) is a subclass of Object that declares many of the attributes and behaviors common to the GUI components in packages java.awt and javax.swing. Most GUI components extend class Component directly or indirectly. Visit java.sun.com/j2se/5.0/docs/api/java/awt/Component.html for a complete list of these common features.

Class Container (package java.awt) is a subclass of Component. As you will soon see, Components are attached to Containers (such as windows) so the Components can be organized and displayed on the screen. Any object that is a Container can be used to organize other Components in a GUI. Because a Container is a Component, you can attach Containers to other Containers to help organize a GUI. Visit java.sun.com/j2se/5.0/docs/api/java/awt/Container.html for a complete list of the Container features that are common to Swing lightweight components.

Class JComponent (package javax.swing) is a subclass of Container. JComponent is the superclass of all lightweight Swing components and declares their common attributes and behaviors. Because JComponent is a subclass of Container, all lightweight Swing components are also Containers. Some common lightweight component features supported by JComponent include:

  1. A pluggable look-and-feel that can be used to customize the appearance of components (e.g., for use on particular platforms). You will see an example of this in Section 22.6.
  2. Shortcut keys (called mnemonics) for direct access to GUI components through the keyboard. You will see an example of this in Section 22.4.
  3. Common event-handling capabilities for cases where several GUI components initiate the same actions in an application.
  4. Brief descriptions of a GUI component's purpose (called tool tips) that are displayed when the mouse cursor is positioned over the component for a short time. You will see an example of this in the next section.
  5. Support for assistive technologies, such as braille screen readers for the visually impaired.
  6. Support for user-interface localizationthat is, customizing the user interface to display in different languages and use local cultural conventions.

These are just some of the many features of the Swing components. Visit java.sun.com/j2se/5.0/docs/api/javax/swing/JComponent.html for more details of the common lightweight component features.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look


Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover

Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net