Chapter 21. JFC and the Swing Package

  • Java Foundation Classes

  • All About Controls (JComponents)

  • Swing Threads—A Caution!

  • Swing Components

  • More About Swing Components

  • Further Reading

  • Exercises

  • Some Light Relief—The Bible Code

In Chapter 20, we saw that the basic idea behind Java GUI programs is that you perform the following actions:

  • Declare controls. You can subclass them to add to the behavior, but this is often unnecessary.

  • Implement an interface to get the event handler that responds to control activity.

  • Add the controls to a container. Again, subclassing is possible but frequently unnecessary.

Chapter 20 explained how to handle the events that controls generate. This chapter dives into the details of the controls themselves: what they look like, how you use them, and what they do. Chapter 22 wraps up by describing containers and how you use them to lay out controls neatly on the screen.

Java Foundation Classes

Supporting a Java interface to the underlying native window libraries achieves the goal of making Java GUI programs highly portable, but it comes at the cost of inefficiency. Peer events must be translated into Java events before they can be handled by Java code. Worse, native libraries aren't identical on each platform, and sometimes the differences leak into the Java layer.

The Java Foundation Classes (JFC) are a set of GUI-related classes created to solve the AWT problem of platform idiosyncrasies. JFC also supports the following items:

  • A pluggable look-and-feel, so that when you run the program, you can choose whether you want it to look like a Windows GUI, a Macintosh GUI, or some other style.

  • An accessibility API for features like larger text for the visually impaired.

  • The Java 2-D drawing API.

  • A drag-and-drop library and an “undo last command” library.

  • The Swing component set.

The Swing components (scroll bar, button, textfield, label, etc.) replace the AWT versions of these components. The AWT is still used for the other areas of GUI functionality, such as layout control and printing. We're describing and working with Swing/JFC from here on.

JFC was bundled with JDK 1.2 as a standard part of Java and was also available unbundled for JDK 1.1. Although it's a core library now, the “x” in the package name in JDK 1.2, javax.swing, reflects the fact that the package first became available as an optional extension library. There was quite a bit of churn over the correct package name for Swing, and the unbundled version for JDK 1.1 was released as package com.sun.java.swing. You should use Swing, rather than AWT components, in new programs you write. All browsers now support Swing through the use of the plug-in.

The Java Foundation Classes are aimed squarely at programmers who want to build enterprise-ready software at least as good as (often better than) software built with native GUI libraries. The JFC has the additional advantage of being a lot simpler than competing window systems and producing code that runs on all systems. It is also future-proof. Your programs won't stop working on your next OS change.

Some terminology

In Win32, the term control means the group of all the GUI things that users can press, scroll, choose between, type into, draw on, and so forth. In the Unix XWindows world, a control is called a widget.

Control and widget are not Java terms. Instead, we use the term component, or, when talking specifically about Swing, the JComponent subclass of Component. Each Swing control is a subclass of javax.swing.JComponent, so each control inherits all the methods and data fields of JComponent. JComponents are serializable, meaning the object can be written out to disk, can be read in again later, and can become an object again. They follow the rules for JavaBeans, so they can be coupled together in visual builder tools.

This chapter is about explaining the Swing components and how you use them. You will build up your GUI programs using these components. The first thing to note is that the Swing components are no longer peer-based, but are written in Java and are thus consistent on all platforms. Under the AWT, a button ended up being a Win32 button on Windows, a Macintosh button on the Macintosh, and a Motif button on Unix. With Swing, the button is rendered on a little bit of screen area that belongs to some ancestor Java component, and Swing puts all the button semantics on top of that. Swing draws the button so it looks armed (ready to push), pushed, or disabled. Because the code is written in Java, the button has identical behavior no matter where it runs.

JFC is quite a big topic. There are more than three hundred classes in the Swing library alone. We'll present nine or 10 individual Swing JComponents here, and provide pointers on the rest. This amounts to several pages, so I recommend you read one or two in depth, then just look at the figures to get an idea of what each does. Return to the appropriate section in the chapter as you need actual code examples.

Overview of JComponent

Object-oriented programming fits well with window systems. The concept of making new controls by subclassing existing ones and overriding part of their behavior saves time and effort. Another similarity is the way that controls have to handle events just as objects handle messages. (Method calls are equivalent to sending a message to an object, and some OOP languages refer to making a method call as “sending a message”).

There is an abstract class called JComponent, which is the parent class for most things that can appear on screen. The basic ingredients of a GUI are all subclasses of the class called JComponent. JComponent is the superclass that holds the common information about an onscreen control and provides higher-level features common to each control, as shown in the following list:

  • Size (preferred, minimum, and maximum).

  • Double buffering (a technique to make frequently changing components look smoother with less flickering).

  • Support for accessibility and internationalization.

  • Tooltips (pop-up help when you linger on a JComponent).

  • Support for operating the control with the keyboard instead of the mouse.

  • Some help for debugging by slowing component rendering so you can see what's happening.

  • The thickness of lines or insets around the edge of the control.

Components correspond to “things that interact with the user” and containers are “backdrops to put them on”. The superclass of javax.swing.JComponent is java.awt.Container. The parent of container is the java.awt component.

The behavior and appearance of each specific control is one level down in the subclasses of JComponent. The Swing lightweight controls can be divided as shown in Table 21-1. We will look at just one or two components from each of these categories.

Table 21-1. Swing lightweight controls

GUI category

Control

Swing class name

Basic controls

Button

Combo box

List

Menu

Slider

Toolbar

Text fields

JButton, JCheckBox, JRadioButton

JComboBox

JList

JMenu, JMenuBar, JMenuItem

JSlider

JToolbar

JTextField, JPasswordField,

JTextArea, JFormattedTextField

Uneditable displays

Label

Tooltip

Progress bar

JLabel

JToolTip

JProgressBar

Editable displays

Table

Text

Tree

Color chooser

File chooser

Value chooser

JTable

JTextPane, JTextArea,JEditorPane

JTree

JColorChooser

JFileChooser

JSpinner

Space-Saving Containers

Scroll pane

Split pane

Tabbed pane

JScrollPane, JScrollBar

JSplitPane

JTabbedPane

Top-Level Containers

Frame

Applet

Dialog

JFrame

JApplet

JDialog, JOptionPane

Other Containers

Panel

Internal frame

Layered pane

Root pane

JPanel

JInternalFrame

JLayeredPane

JRootPane

Each control has methods appropriate to what it does. The JFileChooser control has methods to get and set the current directory, get the selected filename(s), apply a file name filter, and so on. We'll examine these individual JComponents later in the chapter.

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

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