Manning Publications, Inc
Total Page:16
File Type:pdf, Size:1020Kb
Preface An “early technology adopter” (also known as a techno-geek) like myself is usually ready to evaluate an exciting new API the day it is released (if not sooner). In late 1994, while attending a software developers’ conference in California, I encountered a past acquaintance who, at the time, was employed by Sun Microsystems. As I am apt to do, I began complaining to him about the generally inadequate state of the business, and, in particular, the coding effort and portability weaknesses in languages such as C and C++. After listen- ing to my droning for a while, my friend told me about a particularly interest- ing programming language coming from Sun Microsystems in just a couple of months. A new language from Sun? Aren’t they a UNIX workstation com- pany? What does a hardware company know about building computer lan- guages? I was skeptical about trying to use any language produced by Sun Microsystems. The language my friend was referring to was Java 1.0, which, as he pre- dicted, was shipped at the beginning of 1995. In late January of 1995, I con- nected to Sun’s World Wide Web site, pulled down the first version of the Java Development Kit (JDK), and immediately began tinkering. Although I didn’t yet understand the overall vision of Java, I was impressed with the platform independence, and with the speed at which I could build relatively complex prototypes. In these early stages, I viewed Java as a replacement for Microsoft’s Visual Basic as a prototyping tool; however, I was soon to appreci- ate the capabilities of this new language. Java 1.0 was still far from that uto- pian software world that I was searching for, but it demonstrated some real promise as an object-oriented language, and Sun’s commitment to future xx PREFACE enhancements convinced me that Java was the language for me. In spite of the many skeptics around me, I was hooked. I continued to use Java 1.0, noting its weaknesses and doing my best to work around them. I eventually discovered some serious limitations in Java, but, when I was almost ready to give up on the language, Sun released Java 1.1 featuring a totally new event model and offering significant new features like internationaliza- tion and keyboard support. As an early technology adopter, I downloaded the 1.1 release, and my confidence in Java was renewed. Applications that crawled in the Java 1.0 Virtual Machine (VM) raced, once they were adapted to the new event model. However, like many other early adopters, I became exceedingly annoyed with the lack of power of Java’s Abstract Windowing Toolkit (AWT). Still, I per- sisted because, in my mind, Java’s potential was so great. My interest in Java continued, and, in April 1997, I attended my first JavaOne conference, quickly getting lost among the thousands of other attendees. The con- ference spanned all levels of developer expertise, and much of what I saw was famil- iar to me. However, I heard many people (mostly Sun and JavaSoft employees) talking about something code-named Swing. The (as yet) unreleased Swing was Sun’s attempt to improve Java’s AWT and standardize the user interface compo- nents for future versions of Java. Discarding my original plans for the conference, I attended every session concerning Swing so that I could learn more about Sun’s new Swing class library. Most of these sessions were primarily promotional, but a few of the Swing demos caught my eye. By June 1997, Sun finally made a public alpha release of Swing, which coincided with the start of a software project for which I wanted to employ Java. After consid- ering several user interface (UI) class libraries from third-party vendors and com- pletely disregarding Sun’s warnings that the API would change, I decided to take a big risk and use Swing for my project. Through several subsequent alpha and beta releases of Swing, my project paralleled its progress. Each time Sun offered a new release, I had to spend a day fixing my code broken by the changes they made. It was often frustrating, but I was steady in my resolve to integrate Swing into my application. My persistence paid off. My application shipped about the same time that Sun published the final release of Swing, and my work received a very positive response from my peers. While still basking in my success, I began to realize that I had invested a significant number of hours learning to make effective use of the Swing classes. So, as an early technology adopter who had successfully conquered the learning curve, I decided to share my knowledge with others, now that the Swing PREFACE xxi technology was in the mainstream. The results of my efforts are presented in this book, and it is my sincerest hope that it will help smooth out some of the bumps that developers may hit when starting to use Swing. In late February 1998, Sun shipped the finished Swing code; however, Swing is a significant part of a larger group of technologies named Java Foundation Classes (JFC). For developers using Java 1.1, JFC is available for downloading as a separate package from JavaSoft’s web site. Java 1.2 users will discover that JFC is integrated into the Java run time, so no additional installations will be required. Intended audience This book is intended to help Java developers who want to build user inter- faces with Java, regardless of their level of expertise. However, this book is not a Java tutorial, so, if you are a novice, you will certainly require some addi- tional references to assist with the basic language syntax. In the appendices of this book, you will find several good Java language resources. If you are an expert user, this book will serve as a resource which you can keep close at hand, referring to it when you hit a real snag. Regardless of your level of expertise, I will assume that you have some experience building simple applications and/or applets with version 1.1 (or later) of Java. One of the early chapters of this book is a Java AWT refresher course, but it is by no means a complete tutorial for the language. This chapter will serve only to help contrast Java, as it was with AWT, to Java as it now exists with Swing. Since the last chapter of this book will discuss optimization of Java code using multithreading, I will also assume that you have a solid understanding of how to write Java code with threads and of the inner workings of multithread- ing in general. Multithreading is a complex mechanism which can be either your most indispensable optimization tool or your worst debugging night- mare. You must use extreme caution when writing multithreaded applications, and, though we will examine multithreading from the Swing perspective, this book will not help you understand the pitfalls that multithreading can intro- duce into a Java application. Finally, I will assume that you possess a thirst to learn. Though this book will cover some advanced topics related to Swing, there will still be much to INTENDED AUDIENCE xxiii learn on your own. Swing is brimming with interesting classes that can save you hours of work, but to adequately describe each individual class would require a much larger book. Don’t be afraid to throw together some small test programs to test aspects of the Swing classes which you do not immediately understand. How this book is organized This book consists of three parts. We begin with a bit of background on Java and Swing, then look at the basic user interface functionality. Finally, we exam- ine some of the more advanced capabilities of Swing. I’ve also included a chapter to help you with optimization. The book is organized as follows: Part I contains all of the information you require to get started with Swing, including a description of the software packages you need and how to set them up. We will also have a quick Java refresher course for those who need it. Then, we will examine the architecture of Swing and start to talk about some of the advanced capabilities it offers. Part II contains details about the typical classes you will utilize in the pro- cess of creating your own Swing applications. We will look at each of the com- mon components and how to best use them, as well as techniques that can extend and enhance them to better suit your needs. In Part III, we look at more advanced topics. We will describe how to cre- ate components with a custom look-and-feel, and take a look at several ways to make your Java code run faster. NOTE: Throughout this book, you will see sections containing partial information regarding the Swing component APIs, including brief descrip- tions of the contents of significant groups of methods. Although you can find complete information in the online documentation provided with the JFC and JDK toolkits, the partial reference allows you to quickly review the API for a given class without requiring access to a computer. Conventions In order to present the information contained in this book in a consistent manner, the following conventions are used throughout: The text contains many guidelines, recommendations, and warnings to indicate where you might want to pay some special attention. For example: Though significant in scope, Swing represents only one of many parts of a larger technology group called Java Foundation Classes (JFC).