|
There is a newer edition of this item:
|
After a tour of the JFC/Swing packages, the author jumps right into building basic Swing applications, focusing on layout managers (which control the arrangement of JFC components onscreen) and basic graphics programming. The book aims to let you create sophisticated Java applications, and so the author discusses topics such as menus and the toolbar, keyboard handling (and mouseless operation), and dialog boxes to let you do this. The book also covers powerful Swing components, the tree and table controls. The author even looks at creating multiple document interface (MDI) applications. --Richard Dragan
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.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:
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 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.
Product Details
Would you like to update product info or give feedback on images?
|
|
Share your thoughts with other customers:
|
||||||||||||||||||||||
|
Most Helpful Customer Reviews
8 of 8 people found the following review helpful:
5.0 out of 5 stars
Great JFC Book,
By JonP (USA) - See all my reviews
This review is from: Core Java Foundation Classes (Paperback)
Outstanding. I was a beginner with Java & JFC. This book took me through intermediate & up to expert level. JFC underlying structure is explained early on. Each new component family is explained in great detail & lots of high-quality examples. I am a very tough critic when it comes to these sw books, and this was the first one I actually wanted to write the author & thank him/her for such a great job. I couldn't stop reading it! This book is true gold. Only negatives: some minor typos; covered beta of JFC (swing) so once in a while an api changed - this did not hinder the effectiveness of the book (just refer to the Java API when in doubt).
3 of 3 people found the following review helpful:
5.0 out of 5 stars
Best Swing book, the only Swing book that you need.,
By Dean Tran (San Diego, USA) - See all my reviews
This review is from: Core Java Foundation Classes (Paperback)
I am a newbie in both java and Swing world, and have auctioned at least 10 Swing books, until I find this one...Topey UNDERSTANDS Swing very well and it makes difference. He also takes great care of writting this book, he provides clear building blocks to build a Swing application; and nothing is overlooked--e.g. what are RGB number and how to manipulate it....In the first few chapters, he provides many details about Swing which require time to digest before most reader can advance to the next. The book is printed on high quality paper and is inexpensive for its contents. It serves both as tutorial for novices and as reference for advanced programmer. I own many technical books in different fields, most of them are mediocre, some are good, few are great. Overall, This book is exceptional.
3 of 3 people found the following review helpful:
5.0 out of 5 stars
Excellent, complete, well-written,
By A Customer
This review is from: Core Java Foundation Classes (Paperback)
This may be the best technical book I've ever read on any topic. All aspects of Swing are covered in thorough and carefully explained detail. This excellent book promotes a deep understanding of what is going on in Swing, and despite its depth of treatment, it is so well-organized and well-written I believe it is suitable for beginners as well. The examples are numerous and highly instructive. The author keeps the material up to date with changes in Swing through addenda on his web site. One note of caution -- the book only covers Swing, not the entirety of JFC as the title suggests.
Share your thoughts with other customers: Create your own review
|
4.0 out of 5 stars
It is a great book.
I learned a lot from this book.
Published on October 31, 1999
|
Suggested Tags from Similar Products(What's this?)Be the first one to add a relevant tag (keyword that's strongly related to this product).
|
|
This product's forum
Active discussions in related forums
Search Customer Discussions
|
Related forums
|