Core Java Foundation Classes

Core Java Foundation Classes

5.0 1
by Kim Topley, Gary Cornell, Cay S. Horstmann
     
 

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.

Overview

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 newseries 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!

Product Details

ISBN-13:
9780130803016
Publisher:
Pearson Education
Publication date:
07/01/1998
Series:
Core Series
Edition description:
Older Edition
Pages:
1300
Product dimensions:
7.07(w) x 9.24(h) x 1.79(d)

Read an Excerpt

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 ...

Meet the Author

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >