Chapter 27. The JFC, or Swing

The Java Foundation Classes (JFC, or Swing) are a complete set of lightweight user interface components that enhance, extend, and, to a large degree, replace the AWT components such as buttons, panels, list boxes and check boxes. In addition to the buttons, lists, tables, and trees in the JFC, you will also find a pluggable look and feel that allows the components to take on the appearance of several popular windowing systems. The JFC actually uses a few common design patterns that we used for most of the examples in this book. Thus in this section, we review how to use the JFC and point out a number of patterns it encompasses. If you haven't used these powerful classes yet, this is a simple introduction. If you have, read through this chapter to see how many patterns you can discover in them.

We should note at the outset that this package was called "Swing" during development, and it was intended that it be referred to as "JFC" upon release. However, the nickname has stuck, and this has led to the Java programmer's explanation, "It's spelled JFC, but it's pronounced Swing."

Installing and Using Swing

All programs that are to use Swing must import

import javax.swing.*;

and might require one or more of the following, depending on the components used:

import javax.swing.event.*;
import javax.swing.border.*; 
import javax.swing.text.*;

Ideas behind Swing

The Swing components are called lightweight because they don't rely on native user-interface components. They are, in fact, 100 percent pure Java. Thus a Swing JButton does not rely on a Windows button or a Motif button or a Macintosh button to implement its functionality. The components also use fewer classes to achieve this interface than did the heavier-weight AWT classes. In addition, Swing has many more user-interface components than did the AWT, including image buttons, hover buttons, tooltips, tables, trees, splitter panels, and customizable dialog boxes.

Because Swing components create their look and feel completely within the Swing class hierarchy, you can have a pluggable look and feel to emulate Windows, Motif, Macintosh, or the native Swing look.

Swing components also use an architecture derived from the MVC design pattern discussed in Chapter 1. Recall that the MVC pattern keeps the data in a Model class, displays the data in a view class, and varies the data and view using a Controller class. We'll see that this is exactly how the JList and JTable handle their data.

The Swing Class Hierarchy

All Swing components inherit from the JComponent class. JComponent is much like the AWT's Component class in its position in the hierarchy; however, it provides the pluggable look and feel. It also provides

  • keystroke handling that works with nested components,

  • a border property that defines both the border and the component's insets,

  • tooltips that pop up when the mouse hovers over components, and

  • automatic scrolling of any component when placed in a scroller container.

Because of this interaction with the user interface environment, Swing's JComponent is actually more like the AWT's Canvas class than its Component class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset