Home > Store

Core Java Foundation Classes

Register your product to gain access to bonus material or receive a coupon.

Core Java Foundation Classes

ValuePack

  • Sorry, this book is no longer in print.
Not for Sale

Description

  • Copyright 1998
  • Edition: 1st
  • ValuePack
  • ISBN-10: 0-13-080301-4
  • ISBN-13: 978-0-13-080301-6


8030A-5

The long-awaited Java Foundation Classes (JFC) greatly expand Java's graphics capabilities and streamline the construction of state-of-the-art user interfaces. Core Java ™ Foundation Classes offers the detailed information experienced developers need to make the most of JFC-whether they're migrating from AWT or building Java GUIs for the first time.

Using extensive real-world examples, Kim Topley introduces the new Swing classes. You'll learn what the Swing classes are, what they allow you to accomplish, and how they work together. You'll walk step by step through the development of JFC-based applications and components and master such crucial JFC concepts as pluggable look-and-feel.

From creating your application's shell with the JFrame class to developing your own look-and-feel, Core Java ™ Foundation Classes is the most comprehensive programmer's guide to sophisticated JFC-based development. (NOTE: examples use Swing 1.0.1 with JDK 1.1 and JDK 1.2 Beta 3)

STATE-OF-THE-ART INFORMATION FOR JFC DEVELOPERS.

  • GUIs and applications: dialog boxes, menus, toolbars and beyond.
  • Enhanced user interface components, including borders, progress bars, list and combo boxes.
  • In-depth coverage of the Swing text components, including how to display and edit HTML and Rich Text Format (RTF) files.
  • Trees and Tables, including how to extend and customize them.
  • Changing the look-and-feel of a component or of an entire application.

BY PROGRAMMERS, FOR PROGRAMMERS.

Series editor Gary Cornell, co-author of the wildly successful Core Java, brings his diverse experience and perspective to a new series of books written by serious programmers for serious programmers. Like Core Java, every Core Series book:

  • DEMONSTRATES how to write commercial quality code.
  • FEATURES dozens of nontrivial programs and examples-no toy code!
  • FOCUSES on the features and functions most important to real developers.
  • PROVIDES objective, unbiased coverage of cutting-edge technologies-no hype!

Sample Content

Downloadable Sample Chapter

Click here for a sample chapter for this book: 0130803014.pdf

Table of Contents



Who This Book Is For.


What You’ll Need.


How This Book Is Organized.


Conventions Used in This Book.


About the CD-ROM.


A Note on Swing Versions.


Feedback.


Further Information.


Acknowledgments.

I. FROM AWT TO SWING.

1. Introduction to the Java Foundation Classes.

What Are the Java Foundation Classes? Overview of JFC Features. The Swing Packages. The Model View Controller Architecture. Summary.

2. Frames, Labels and Buttons.

The Shell of an Application: The JFrame Class. Components and Their Properties. Containers and Layout Managers. Event Handling. Using Buttons. Images on Components: Icons. Summary.

3. Managing the User Interface.

Layout Managers. Working with the Application Window. Summary.

4. Graphics, Text Handling and Printing.

Working with Graphics. A Graphics Application. Rendering Text. Printing Graphics and Text. Summary.

II. USING SWING TO BUILD BETTER APPLICATIONS.

5. Keyboard Handling, Actions and Scrolling.

Implementing Mouseless Operation. Scrolling Components. Logical Scrolling and Forcing Visibility of an Area. Implementing Scrolling with JScrollBar. Scrolling with the Keyboard. Summary.

6. Menus and Toolbars.

The Menu Bar and the Menu System. Toolbars. Summary.

7. Using Standard Dialogs.

Basic Dialogs. The File Selection Dialog. Summary.

8. Creating Custom Dialogs.

Creating Custom Dialog Boxes. Tabbed Panes and Borders. Swing Components Often Used in Dialogs. Summary.

III. ADVANCED TOPICS.

9. Text Controls.

Simple Text Controls. Text Components: Under the Hood. Getting Multiple Views of a Document. Documents with Nontext elements: JTextPane and JEditorPane. Summary.

10. The Tree Control.

The Tree Control. Custom Tree Rendering and Editing. Summary.

11. The Table Control.

An Overview of the Table Control. The Table Model. Table Columns and the Table Column Model. The Table Control. Customized Table Rendering and Editing. Summary.

12. Multiple-Document Applications.

Internal Frames. Layered Panes. Working with Internal Frames. Icons, the Desktop Pane and the Desktop Manager. An Example Multiple- Document Application. Summary.

13. The Pluggable Look-And-Feel.

The Look-and-Feel and the UIManager. Changing Component Look- and-Feel. Summary.

Appendices.
A Class Diagrams.
B Swing Components and Multithreading.
Index.

Preface

The term Java Foundation Classes (JFC) refers to a collection of features that are included in, or can be used with, version 1.1 of the Java Development Kit (JDK) together with some new features introduced by JDK 1.2. The JFC is such a large topic area that to cover it in one volume and give it the treatment that it deserves, and that an experienced programmer would expect, is an impossible task. So, this book doesn't attempt to describe everything that falls under the JFC banner. Instead, I have chosen to concentrate on the parts that are of most immediate interest to programmers working in the commercial Java development environment today and left out those pieces that are not yet stable or would take up too much space in this volume. Most of this book is, therefore, concerned with the subset of the JFC that you can use with both JDK 1.1 and JDK 1.2. Specifically, this means the AWT as it exists in JDK 1.1, which is briefly covered as introductory or refresher material, printing, a JDK 1.1 feature that is deemed to be part of the JFC, and the Swing component set, which is the book's prime concern.

In writing about Swing, I have tried to include material that is of most interest to programmers who will want to use it to create real applications. The approach taken throughout is to explain how the Swing components and the various new Swing mechanisms (such as keyboard accelerators) work, by giving an overview of each topic and then presenting examples that show the basic principles. Then, typically, I move on to show more complex use of the component or feature and how it can be used together with other components and features to solve real application- related problems. Much of the time, you can find out some of what you want to know by reading the documentation that you get with the JDK or with the Swing package, but this documentation often falls short of explaining the more complex features and how they interact. These are the things that programmers usually need to understand before they can make full use of what is available. You'll find those explanations in this book.

I have tried to write this book as I would have liked to have seen it as an experienced programmer wanting to learn about Swing. By reading the documentation, studying the source code, disassembling the class files and trying various test cases, I have tried to do more than just scratch the surface of this large and complex topic by gathering together material and examples that are not available in Sun's documentation and presenting it in an organized, focused manner.

Who This Book Is For

This book is for experienced Java programmers who want to find out about the Java Foundation Classes and, in particular, the Swing component set. Because this book is aimed at experienced programmers, you'll find that most of the examples are not trivial, with the exception of some of those in the early chapters that illustrate basic points that can't be embedded in a more complex setting for clarity. Many of the examples that you'll find in this book are intended to be used as a starting point for writing production code that you can include in your own applications and some of the material here has been included specifically to address the most common questions that have been aired in the various news and discussion groups since the early versions of Swing were released. In particular, I have tried to address the following issues that seemed to be causing most confusion:

  • Creating and customizing tree components.
  • Custom renderers and editors for list boxes, trees and tables.
  • How to use the powerful Swing text components, including a detailed description of how they work.
  • Making proper use of the internal frames feature to create multi- document applications.
  • Customizing individual components by creating your own look-and- feel classes.

    If you're looking for examples on how to write slick Java applets with Swing, then this book probably won't suit you. The focus in this book is entirely on writing Java applications. However, if you're already thoroughly familiar with JDK 1.1 applets, you'll find that you can use write applets using Swing very quickly and most of this book will still apply, since Swing components can be used in applets just as easily as in applications.

    What You'll Need

    This book is written with the assumption that you are familiar with the Java language and the core Java classes and that you can compile and run Java applications. Although this book is mainly about the Swing components, the first few chapters cover the basic principles of the Abstract Windows Toolkit (AWT), knowledge of which is a prerequisite for understanding Swing. You'll get most from this book if you have read Core Java 1.1, Volume I—Fundamentals by Cay S. Horstmann and Gary Cornell, also published by Prentice Hall, and you understand inner classes, which are heavily used both in the Swing source code and in the examples shown in this book. Core Java includes excellent coverage of this topic.

    Assuming that you have a computer running Windows 95, Windows NT version 4 or later, or a version of Solaris supported by JDK 1.1 or JDK 1.2, almost everything else that you need, including versions of JDK 1.1 and JDK 1.2 and the Swing components for all of these platforms, is included on the CD- ROM, with the exception of the Swing components for use with JDK 1.1, which you will need to download from the JavaSoft web site, as described in EAbout the CD-ROM” below.. You will, of course, need some kind of basic development environment. However, a basic text editor that you can invoke from the command line is sufficient.

    How This Book Is Organized

    The book is divided into three sections. Section 1, “From AWT to Swing,” (Chapters 1 through 4) describes the relationship between the Abstract Windows Toolkit and Swing. Because Swing is a development based on the AWT, some understanding of the AWT is needed before you can fully utilize the Swing components. Although there are many good books on the AWT, this book opens by presenting the fundamentals for those who are not entirely confident that they fully understand them and as revision for those who do. These early chapters are, however, written not simply as a short course on the AWT — they are written with a Swing slant to them so that as early as Chapter 2 you will meet your first Swing components. I recommend that everybody reads Chapter 1. Then, depending on your background, you should read, or at least skim, Chapters 2 through 4.

    Section 2, “Using Swing to Create Better Applications” (Chapters 5 through 8) deals with the various new mechanisms and components that Swing offers that have no real parallel in the AWT. Much of this section deals with individual Swing components or Swing concepts and, to a certain extent, the chapters in this section can be read in any order. Finally, Section 3, “Advanced Topics,” (Chapters 9 through 13) covers larger topic areas such as the tree and table components and the pluggable look- and -feel.

    Chapter 1 introduces the Java Foundation Classes (JFC) and puts them into perspective by comparing them to the AWT. You'll learn the scope of the JFC and get a quick overview of the main features of the Swing component set. The last part of this chap ter introduces you to one of the more novel features of Swing — the so-called “pluggable look-and- feel,” which allows applications to adapt their appearance to suit the platform that they are running on or to take on the same appearance across all plat forms. It also explains the basis of this facility, the Model/View/Controller architecture (MVC).

    Chapter 2 is the first of the AWT revision chapters. This chapter begins by introducing the Swing JFrame class as the top-level window of an application and uses it to illustrate basic AWT concepts such as components, containers, position, size, color, fonts and so on. The second half of the chapter includes material on event handling and introduces Swing's JLabel and JButton classes, which allow you to include text and an image on the same component. Here, you'll also find out about Swing improvements on the AWT, such as icons and accelerator keys, which now work outside of menus, to which they were confined in JDK 1.1.

    Chapter 3 is concerned with global user-interface issues, principal among which is controlling the layout of components in the interface. With this in mind, almost the whole chapter is devoted to in-depth coverage of the AWT and Swing layout managers. Layout managers are a topic that has, in the past, been given only fleeting coverage in most Java books and represents a large proportion of the questions that are asked in the Java-related Internet newsgroups. Here, I have tried to create a consistent picture of all of the layout managers and to address the most common misunderstandings by illustrating how each layout manager lays out its container and how resizing the container affects the positioning of the components.

    Swing includes a useful new feature that can help with understanding problems with component layout—Graphics Debugging. This feature allows you to see how your components are drawn, line-by-line, in slow motion. Chapter 3 looks at this feature and shows you how to use it and concludes with a look at the events generated by windows and by individual components.

    Chapter 4 covers the AWT Graphics object and describes how you can use it to render text and draw graphics. The main body of this chapter is devoted to building a complete Swing application that allows you to use the mouse to draw and fill colored graphic shapes. While constructing this application, you'll put into practice much of what was covered in the previous two chapters and you'll also discover how to create custom icons and components with borders. The completed application will serve as a starting point for several improvements that will be made to it in rest of the book. Chapter 4 closes with a discussion of printing, another topic that has caused much confusion in the past.

    Chapter 5 shows you how to develop applications that work well when the user prefers to use the keyboard rather than the mouse and introduces focus handling and the powerful concept of Actions. Actions are often used in Swing to abstract an action to be performed from the stimulus to perform itÑsuch as a keystroke or a button press. The second half of the chapter looks at how Swing supports scrolling using the new JScrollPane and JScrollBar components, which are much more powerful than their AWT counterparts.

    Chapter 6 introduces the Swing menu system. Swing menus are more consistent than those available with AWT. Keyboard accelerators are available on any menu or menu item and you can use icons as well as, or instead of, text. YouÕll also see how to integrate the Actions that were introduced in Chapter 5 with the menu system and with another new Swing facility, the toolbar.

    Chapter 7 is the first of two chapters that contain extensive coverage of dialogs. This chapter concentrates on dialogs that you can easily create using the JOptionPane component. These dialogs allow you to post error, warning and information messages or present options to the user and wait for a choice to be made. Two more useful standard dialogs, the file chooser and color chooser, are also covered in this chapter.

    Chapter 8 shows you how to use the Swing JDialog class to create your own dialogs. This chapter also introduces many of the Swing components that are useful in dialogs, such as tabbed panes, list boxes, combo boxes, progress controls and sliders.

    In Chapter 9, you'll find an in-depth description of the Swing text controls, starting with the simple JTextField and JTextArea components, which are related to the AWT TextField and TextArea controls. The Swing controls are underpinned by a complex but powerful document model that is covered in detail in this chapter. The description of the document model is followed by a description of two new Swing text controls — JTextPane, which allows multiple fonts, colors, images and event components to be mixed in a view of a document and the flexible JEditorPane, which adapts itself to render documents held as plain text, HTML, Rich Text Format and a potentially infinite number of other formats.

    Chapter 10 describes the Swing tree control and shows you how to build a tree, how to control which parts of it are expanded or collapsed and how to traverse the tree in various different ways. This chapter also shows you a custom control based on the tree that presents a file system in a manner familiar to users of the Windows platform and it describes in detail how to customize trees using specially-developed renderers to control how the various pieces of the tree are drawn.

    Chapter 11 is concerned with the table component. This component allows you to present data in a tabular form. You'll see how to determine the order of columns and their sizes and how to change the way in which the data in the table is displayed. There is also a section on table cell editors, which shows you how to present to the user a suitable interface for changing the content of the table.

    Chapter 12 discusses SwingÕs support for multiple-document applications. Starting with a discussion of the basic components that make multiple- document applications possible, the chapter moves on to discuss desktop panes and icons and the desktop manager. You'll also see how to create a custom desktop pane that automatically cascades or tiles the windows that it manages and how to customize the desktop manager itself.

    Finally, Chapter 13 returns to the theme of the pluggable look-and-feel that was first touched on in Chapter 1. This chapter shows the underlying support for changing the look-and-feel of an application, then shows you how to create your own user interface for the Swing button component.

    Finally, you'll see various ways to introduce look-and-feel related customizations into your application and how to create your own look-and- feel.

    This book covers all of the Swing components and most of the other important Swing features. For ease of reference, the following table lists the Swing components, along with the chapters in which they are described.
    Component Chapter Component Chapter
    Box 3 JRadioButtonMenuItem 6 BoxLayout 3 JRootPane 2 JButton 2 JScrollBar 5 JCheckBox 8 JScrollPane 5 JCheckBoxMenuItem 6 JSeparator 8 JComboBox 8 JSlider 8 JComponent 2 JSplitPane 9 JDesktopPane 12 JTabbedPane 8 JDialog 8 JTable 11 JEditorPane 9 JTextArea 9 JFrame 2 JTextField 9 JInternalFrame 12 JTextPane 9 JLabel 2 JToggleButton 8 JLayeredPane 12 JToolBar 6 JList 8 JToolTip 8 JMenu 6 JTree 10 JMenuBar 6 JViewport 5 JMenuItem 6 JWindow 2 JOptionPane 7 OverlayLayout 3 JPanel 2 ProgressMonitor 8 JPasswordField 9 ProgressMonitorInputStream 8 JPopupMenu 6 Timer 8 JProgressBar 8 UIDefaults 13 JRadioButton 8 UIManager 13

    Conventions Used in This Book

    Courier font is used to indicate Java code, both in the listings and in the shorter code extracts that you'll find included in the text. The same font is also used to indicate key words and class names (such as JFrame). In some cases, we show a code extract and then explain how to modify it to change its behavior. In this case, the code that is added or modified is shown in bold courier font.

    Icons are used to call out material that is of significance and that the reader should be alerted to:
    Core Note, Alert, Tip
    Note: This is information that deserves special attention, such as an interesting fact about the topic at hand, or that the reader may want to keep in mind while programming.
    Alert: This is information that, while useful, may cause unexpected results or serious frustration.
    Tip: This is particularly useful information that will save the reader time, highlight a valuable programming tip, or offer specific advice on increasing productivity.

    About the CD-ROM

    The CD-ROM that accompanies this book has the following directory structure:
    COREJFC JDK1.1 CJFC11.ZIP JDK1.2 CJFC12.ZIP WINDOWS JDK1.1 JDK1.2 SOLARIS JDK1.1 JDK1.2

    The COREJFC directory contains the source code and compiled class files for the examples in this book. The WINDOWS directory contains a version of JDK1.1 for Windows 95 and Windows NT (version 4 or later) and a beta version of JDK 1.2 for Windows platforms. The SOLARIS directory has the same software for use with the Solaris operating system.

    Installation Instructions

    Since Swing can be used either with JDK 1.1 or JDK 1.2, you have a choice as to how to use the CD-ROM. If you want to use Swing with JDK 1.1, you need to do the following:
  • Install the appropriate version of JDK 1.1 for your platform.
  • Download and install the appropriate version of Swing for your platform.
  • Install the JDK 1.1 examples.
  • Optionally compile the example source files.

    On the other hand, if you are going to use Swing with JDK 1.2, then you should:
  • Install the version of JDK 1.2 appropriate for your platform.
  • Install the examples for JDK 1.2.
  • Optionally compile the example source files.

    Because Swing is an integral part of JDK 1.2, you donÕt need to download the Swing package if you intend to work with JDK 1.2.

    The installation instructions in this section use the Windows platform when presenting typical command lines. If you are installing on the Solaris platform, you should amend the command lines as appropriate.

    Installing for JDK 1.1
    Install the JDK

    If you already have JDK 1.1 installed, you can skip this step. However, you must install a new JDK if you do not have version 1.1.5 or higher, because Swing is not supported on earlier versions. To install the JDK, proceed as follows:
    1. Locate the subdirectory on the CD-ROM that matches your operating system (WINDOWS\JDK1.1 or SOLARIS/JDK1.1). In this directory, you will find an installation file for the JDK in a format that is appropriate for your platform. For example, for Windows, the JDK will be in a .exe file.

    2. Install the JDK files onto your computer by executing the installation file. We recommend that you place the JDK in a directory called jdk. If you have an older version of the JDK, completely remove it or move it to a new location before installing the newer version.

    3. Add the jdk\bin directory to your PATH variable. For example, under Windows 95, if you install the JDK in the directory c:\jdk, then place the following line at the end of your AUTOEXEC.BAT file:
    PATH=c:\jdk\bin;%PATH%

    Core Note

    The JDK setup program will offer a default location which is different for each JDK version, such as jdk1.1.5. We recommend that you install the JDK in the directory jdk, but if you are using multiple versions, go ahead and accept the default. Be careful to modify the remaining instructions in this section to reflect your choice, however.

    Download and Install Swing

    If you don't already have a copy of the Swing components for JDK 1.1, you should download them from the JavaSoft web site at http://java.sun.com. At the time of writing, the following URL will take you to the download page for Swing: http://java.sun.com/products/jfc/index.html

    The Swing package comes as a ZIP archive which you can install simply by unpacking its contents. Choose a suitable location and unpack the archive using any utility that can handle ZIP files. If you donÕt have a native utility for your platform, you can use the JDK jar tool to unpack the archive but, in this case, you should first reboot your computer if you have just installed the JDK.

    Having installed Swing, you need to add the appropriate JAR files to your CLASSPATH. Swing contains several JAR files, all of which are found in the swing-1.0.x directory:
    swing.jar windows.jar motif.jar multi.jar beaninfo.jar swingall.jar

    Core Note

    The name of the Swing directory will depend on the current version at the time that you download it. Here, we use the generic name swing-1.0.x. You should substitute the appropriate name for the version you download -for example, you might need to use swing-1.0.2.

    You can either choose to include the minimum set of files for your platform, or you can simply add swingall.jar to your CLASSPATH. If you just want to use the standard Metal look-and-feel, you can get away with just adding swing.jar. If you want to use either the Windows or Motif look-and- feel, add windows.jar and/or motif.jar as well. If you plan to develop Java Accessibility features (a topic outside the scope of this book), you need to add multi.jar. You wonÕt need to use beaninfo.jar file unless you need to recreate your own version of swingall.jar — see the README.txt file in the swing-1.0.x1 directory for details.

    If you are going to use Swing in conjunction with a third-party development environment, or you want to be able to use all of the look-and-feels, you should add swingall.jar to your CLASSPATH. Refer to “Set the CLASSPATH variable” below for further details.

    Install the Examples

    The Core JFC example source files for JDK 1.1 are held in the archive COREJFC\JDK1.1\CJFC11.ZIP. To install them, first create a directory, say C:\CoreJFCBook, and copy the archive from the CD-ROM into this directory. If you have a utility for your platform that can handle ZIP files (such as WinZip), you can go ahead and unpack the archive, making sure that the files are all installed in the C:\CoreJFCBook directory. Refer to ÒUnpack the ExamplesÓ below to confirm that you have unpacked the archive correctly. If you do not have such a utility, you can unpack them using the JDK jar tool.

    Set the CLASSPATH variable

    Next, you need to set the CLASSPATH environment variable. This must contain:
  • jdk\lib\classes.zip.
  • The appropriate Swing jar files.
  • The directory into which you copied the Core JFC examples.

    For example, under Windows 95, add a line like this to your AUTOEXEC.BAT file:
    SET CLASSPATH=c:\jdk\lib\classes.zip;c:\swing- 1.0.x\swingall.jar;c:\CoreJFCBook
    If you use the C-shell under Solaris, place the following in your .cshrc file (as one line):
    setenv CLASSPATH /jdk/lib/classes.zip:$HOME/swing-
    1.0.x/swingall.jar:$HOME/CoreJFCBook
    assuming that you have the JDK installed in /jdk and the examples and that the Swing archives have been installed under your home directory.

    Obviously, if your CLASSPATH variable is already set, you will need to modify these instructions accordingly.

    Finally, log out or reboot the computer.

    Unpack the Examples

    If you did not unpack the examples when you installed them, you should do so now. Make the directory in which the CJFC11.ZIP file has been installed (CoreJFCBook) your working directory, then execute the following command:
    jar xvf CJFC11.ZIP

    This will extract all of the examples. You should now have a directory called JFCBook in your CoreJFCBook directory, with one subdirectory per chapter. A typical layout might be as follows:
    C:\CoreJFCBook\JFCBook\Chapter2 C:\CoreJFCBook\JFCBook\Chapter3 C:\CoreJFCBook\JFCBook\Chapter4
    and so on. If you do not have this layout, you have done something wrong and the example programs will not work. One possible problem you might have at this stage is inability to run the jar command or an error while running it. If this is the case, either your PATH variable or your CLASSPATH variable is not correctly set. Check that you have correctly followed the instructions in this section, then reboot your computer to make sure that the settings have been incorporated into your environment.

    Finally, using your favorite editor or file viewer, open any of the Java source files, for example JFCBook\Chapter2\AddExample.java. Near the top of this file, you should see a line like this:
    import com.sun.java.swing.*;
    If, instead, you see this line:
    import java.awt.swing.*;
    then you have installed the examples for JDK 1.2 Remove the CJFC12.ZIP file and all of the other files in this directory, copy the CJFC11.ZIP file from the CD-ROM directory COREJFC\JDK1.1 and unpack it again.

    Installing for JDK 1.2

    Install the JDK

    Installation JDK 1.2 is the same as for JDK 1.1, so refer to “Install the JDK” under “Installing for JDK 1.1.” Note that JDK 1.2 includes the Swing components, so return here after installing JDK 1.2 — do not attempt to install the JDK 1.1 Swing components with JDK 1.2.

    Install the Examples

    The Core JFC example source files are held in the archive COREJFC\JDK1.2\ CJFC12.ZIP. To install them, first create a directory, say C:\CoreJFCBook, and copy the file from the CD-ROM into this directory. If you have a utility for your platform that can handle ZIP files (such as WinZip), you can go ahead and unpack the archive, making sure that the files are all installed in the C:\CoreJFCBook directory. Refer to “Unpack the Examples” below to confirm that you have unpacked the archive correctly. If you do not have such a utility, you can unpack them later using the JDK jar tool.

    Set the CLASSPATH variable

    Next, you need to add the CLASSPATH environment variable. This must contain:
  • jdk\lib\classes.zip.
  • The directory into which you copied the Core JFC examples.

    For example, under Windows 95, add the following line to your AUTOEXEC.BAT file:
    SET CLASSPATH=c:\jdk\lib\classes.zip;c:\CoreJFCBook
    If you use the C-shell under Solaris, place the following in your .cshrc file:
    setenv CLASSPATH /jdk/lib/classes.zip:$HOME/CoreJFCBook
    assuming that you have the JDK installed in /jdk and the examples under your home directory.

    Obviously, if your CLASSPATH variable is already set, you will need to modify these instructions accordingly.

    Finally, log out or reboot the computer.

    Unpack the Examples

    If you did not unpack the examples when you installed them, you should do so now. Make the directory in which the CJFC12.ZIP file has been installed (CoreJFCBook) your working directory, then execute the following command:
    jar xvf CJFC12.ZIP

    This will extract all of the examples. You should now have a directory called JFCBook in your CoreJFCBook directory, with one subdirectory per chapter. A typical layout might be as follows:
    C:\CoreJFCBook\JFCBook\Chapter1 C:\CoreJFCBook\JFCBook\Chapter2 C:\CoreJFCBook\JFCBook\Chapter3
    and so on. If you do not have this layout, you have done something wrong and the example programs will not work. One possible problem you might have at this stage is inability to run the jar command or an error while running it. If this is the case, either your PATH variable or your CLASSPATH variable is not correctly set. Check that you have correctly followed the instructions in this section, then reboot your computer to make sure that the settings have been incorporated into your environment. Finally, using your favorite editor or file viewer, open any of the Java source files, for example JFCBook\Chapter2\AddExample.java. Near the top of this file, you should see a line like this:
    import java.awt.swing.*;
    If, instead, you see this line:
    import com.sun.java.swing.*;
    then you have installed the examples for JDK 1.2. Remove the CJFC11.ZIP file and all of the other files in this directory, copy the CJFC12.ZIP file from the CD-ROM directory COREJFC\JDK1.2 and unpack it again.

    Building and Running the Examples

    The example programs on the CD-ROM have been compiled using JDK 1.1.5 and Swing 1.0.1 and also with JDK 1.2 Beta 3. It is recommended that you just use the compiled class files that you'll find with the source files in the CoreJFCBook directories. If you need to rebuild the example files, perhaps because you want to try out some modifications of your own or to use a later version of the JDK or Swing that has incompatible changes, you should follow the procedure described in this section. The examples for each chapter are held in a directory named after that chapter and all the examples in that directory must be compiled together. You must also build the examples in ascending chapter-number order, apart from those in Chapter 13, which must b e built after those in the directory Cjfc. The correct build order is, therefore, as follows:
  • Chapter 2 through Chapter 12
  • Cjfc
  • Chapter13

    To build the examples for Chapter 2, do the following:
    cd C:\CoreJFCBook\JFCBook\Chapter2
    javac *.java

    This process should be repeated for each chapter, in the order just shown.

    Core Alert

    There are dependencies between the examples for different chaptersÑthe examples for some of the later chapters reuse classes developed earlier. If you compile the examples out of order, you will probably find that your compilations will fails because of missing classes.

    To run the examples, you must have the directory C:\CoreJFCBook on your CLASSPATH. You must then supply the full class name of the example that you want to run along with any required parameters as described in the appropriate chapter. For example, t o run the first example in Chapter 2, use the following command:
    java JFCBook.Chapter2.BasicFrame

    In some cases, you will find explicit instructions that tell you how to run particular examples. Usually, however, you just need to use the exampleÕs class name and its chapter number as shown here.

    A Note on Swing Versions

    Since it was first released in developer form, the Swing API has undergone dramatic changes. Unfortunately, this process is not yet complete. At the time of writing, the current version of Swing for JDK 1.1 is Swing 1.0.1. The same version is also included in JDK 1.2 Beta 3, which you will find on the CD-ROM. The text and examples in this book relate to this particular version of Swing. Because most (over 90%) of the public API was frozen when Swing 1.0.1 was released, what you read in this book will continue to apply to later versions of Swing in almost all cases.

    However, two of the Swing components, JFileChooser and JColorChooser (both of which are described in Chapter 7), did not have their API frozen in Swing 1.0.1. The API of these components is expected to change before JDK 1.2 is released and these same changes will also appear in the JDK 1.1 version of Swing. Be aware, therefore, that if incompatible changes are made to these components, some of the examples on the CD-ROM that use them may not work and their source code may no longer compile with later versions of Swing and JDK 1.2. To keep you up-to-date with the latest changes, there is a Web page at the following URL that you can use to download updated examples, should this become necessary:
    http://www.topley.demon.co.uk/corejfc.html

    Feedback

    No book is perfect and this one is unlikely to be an exception to that rule. Even though it has been through a long period of revision and technical review, there are, of course, errors still to be found and improvements still to be made. If you find an error or if there is something that you think might make the book more useful, we want to know about them. Please send comments and corrections to the following e-mail address:
    kt@topley.demon.co.uk

    Further Information

    Java is a fast-moving subject. To keep up with developments, reading books is not enough. The best place for up-to-date information is, of course, the JavaSoft web site at:
    http://java.sun.com

    You can get the most recent information on Swing and download the latest version of the software from the following page:
    http://java.sun.com/products/jfc/index.html

    JavaSoft also provides an online Swing magazine, called “The Swing Connection,” which is periodically updated and can be found at:
    http://java.sun.com/products/jfc/swingdoc-current

    Finally, there are several active newsgroups devoted to discussion of the Java platform. The following two groups in particular frequently discuss Swing and JFC-related issues:
    comp.lang.java.gui
    comp.lang.java.programmer

    Acknowledgments

    I seem to have spent all of my life buying and reading books. It is probably not too great an exaggeration to say that almost everything I know I have read in a book somewhere, some time. So it was only to be expected that when Java first appeared, I turned to the bookshop to help me find out about it. After examining the (then) limited number of books available on the subject, I settled on the one that stood out as most appropriate for an experienced programmer and took down from the bookshelf the copy in best condition. That book was the first edition of Core Java, by Cay S. Horstmann and Gary Cornell.

    A little over a year later, having decided to try to make some money from books instead of spending money on them, I submitted a very basic outline for a book to a small group of publishers. The next day, I found two e-mail items waiting for me — one from Gary Cornell, the other from Greg Doench at Prentice Hall. Of course, there was a lot more work to do before a contract was agreed, but of all the people that deserve thanks for making this book possible, these two are at the head of the queue. It still amazes me that they were willing to put such faith in someone who had not yet written a book, on the strength of a vague outline and two chapters of a different book idea that never came to fruition. Throughout the writing process, which seems to have taken such a long time, I found that I could always approach either of them with questions or problems and have them addressed quickly. They helped me with all kinds of issues, from determining the scope of the book, right down to the details of the template with which the manuscript was written. But, most of all, they trusted me to deliver and it is for this that I am most grateful to them. As the writing phase drew to a close, Greg introduced me to Jim Markham, the book's development editor. It fell to Jim to read the entire manuscript and turn what was essentially a jumble of programmerÕs language into more fluent sentences and paragraphs, all under pressure of time. I can't thank Jim enough for the attention he paid to this taskÑthe book you now hold in your hands is much more readable than the collection of chapters he started with. As well as being an excellent development editor, I have to say that I used Jim frequently as a sounding board for ideas and asked him to do other things that went well beyond the bounds of his job and was never disappointed.

    On the technical review front, my thanks go to Buzzy Brown (of IBM Retail Store Solutions), Blake Ragsdell and Cameron Laird, who all read parts of the manuscript at various stages of its development, for their comments which have helped improve the presentation of the technical content as well as remove several errors of fact. Special thanks, though, are due to two people who have contributed significantly to the technical side of this book. First, Rayme Jernigan read and commented on the entire manuscript as it was being overhauled for the penultimate time. Called to the task at the last minute, Rayme did an incredible job against a deadline that at first looked aggressive and then almost impossible. Nevertheless, he made it and is still alive to tell the tale! Secondly, I am indebted to Timothy Prinzing of the JavaSoft Swing development team who, at the same time as working on the Swing 1.0 release, graciously agreed to review the chapter describing the Swing Text Components, for which he was responsible. His feedback helped me to include the Swing 1.0 changes much more quickly than would otherwise have been possible and has made this a much more useful and accurate chapter.

    I would also like to thank all the other people at Prentice Hall who helped with the production of this book, especially Mary Treacy for making sure that the contract details were sorted out, Yvette Raven for putting together the book's CD-ROM, and Joe Czerwinski, who answered many questions on the document template and saw the book through the production phase against a tight schedule. Despite the efforts of all of these people, and many others, as with most books of this size, there will undoubtedly be errors that remain. For these, I claim all credit.

    Writing a book of this size takes a long time. In the year or so that I have devoted to this task, there have been times when I was inclined to take a long break or even consider giving up completely. Apart from my own stubbornness (and, of course, a possible breach of contract), several groups of people have inspired me to keep going with this project, possibly without realizing it. Most significantly, I would like to mention my work colleagues, Dave Martin and Tim Lowe, who made it their business to ask about the state of the book almost every day of every week and thus made it totally unacceptable to stop work and my supervisor, Polly Yee, for being understanding when asked for a few days' leave to get through a particularly busy part of the schedule.

    Finally, I would like to express my gratitude to my family for putting up with my effective absence for most of the last year and for not complaining too much about the state of the study, which has been buried under books, notes and computers for the duration of this project.

Updates

New To This Edition

KIM TOPLEY is a freelance computer consultant whose experience includes writing microcode for mainframe computers, device drivers, file systems and security features for the Unix operating system, implementing communications protocol stacks and developing GUI interfaces for end-user applications. He is currently working for a financial institution in London, England.

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020