24-01-2013, 04:22 PM
Introduction to the Java Foundation Classes
JavaFoundatin.pdf (Size: 236.88 KB / Downloads: 290)
What Are the Java Foundation Classes?
The Java Foundation Classes are a group of features whose implementation began
with JDK 1.1 and is continued in JDK 1.2, one of the most significant parts of which,
and the main concern of this book, is the Swing component set. The Swing components
are all graphical user interface controls that replace most of the platform-native
components provided by the JDK 1.0 and JDK 1.1 AWT. The best way to understand
what the Swing components are and how they will affect the development of Java
applications is to look at how the Swing project came about and at the problems with
the AWT that Swing is intended to address.
In the Beginning:The Abstract Window Toolkit
Sun Microsystems released JDK 1.0 in the first half of 1996. A significant part of
this new language was a package called java.awt, which contains the classes for
the AWT.
In its early days, Java was associated very closely with the Internet and, thanks to
its incorporation in Netscape’s popular Web browser, the sight of cool applets written
in Java became commonplace. A Web site without a Java applet of some kind soon
became hard to find. While dedicated surfers searched for the next exciting applet,
programmers were busy trying to meet user expectations using the facilities of the
AWT—for it is the AWT that provides the user interface that, when viewed through
a Web browser, becomes a Java applet.
Most of the original Java applets were of a similar, rather simple, type: moving
images, dancing text, showers of pixels and just about anything that caught the eye.
The aim was usually just to liven up an otherwise static Web page and attract as many
callers as possible, in the hope that at least some of them might look at and, better
still, buy the product or service that the Web site’s owner was offering. Creating such
simple applets did not really place much of a demand on the AWT. Most of the work
revolved around loading and displaying sequences of images or animating some simple
text. More sophisticated applets allowed user interaction using the mouse or the
keyboard but, on the whole, the level of functionality required from the AWT by
these applets was very low.
Alongside the applet developers, others were trying to use Java to develop weightier
applications, such as office productivity tools and database client interfaces. While
the applet developer was very happy with his new-found freedom of expression and
got just about all he needed from the AWT, his colleagues were typically less
impressed. For serious development work, AWT 1.0 simply did not stand up to scrutiny.
For one thing, it was slow.
While this is not such an issue on the Internet, it assumes great importance to a
user accustomed to applications written in C or C++ that usually (but with some
notable exceptions) don’t require you to wait a noticeable amount of time for some-
6 Chapter 1 Introduct ion to the Java Foundat ion Clas ses
thing to happen after pressing a button. For another, the AWT simply wasn’t robust
enough—the implementation, particularly of the Windows version, was buggy and
required developers to spend inordinate amounts of time looking for solutions to
problems that weren’t in their own code and having to produce work-arounds to
patch up their applications and make them usable. Worst of all, though, the AWT didn’t
provide much variety in its range of user interface elements.
User interfaces have come a long way since the days of DOS. These days, even
Unix has a windowing interface and only programmers and (some) system administrators
still toil away at the shell prompt. Over the years, beginning with the introduction
of Windows 3.0 and continuing up to the present day, users have become
accustomed to interacting with applications using a mouse and a collection of familiar
controls such as buttons, scroll bars, text fields and so on. Each release of
Windows or the Motif toolkit brought new elements to the user’s attention, most of
which were quickly accepted and became indispensable.
Unfortunately, AWT 1.0 was not particularly sophisticated in its supply of visual
controls, so developers who wanted to make their Java spreadsheet or word processor
resemble existing products had to start virtually from scratch and write their own
components.
Inevitably, this was a long and tiresome process, repeated in many companies
around the world. The end result, of course, was that there never were that many
serious Java applications developed with AWT 1.0 that made it to the marketplace,
even in Beta form. By the time the development community had come to terms with
creating their own components, JavaSoft had released JDK 1.1 and, along with it, versionof the AWT.
By comparison to its predecessor, AWT 1.1 was a great improvement. The
Windows version was completely rewritten and was made much faster and more
robust. Better integration with the user’s desktop was provided and, for the first time,
a Java programmer could give an application access to a printer without needing to
write platform-dependent native code. The programming model was improved, too,
with a better mechanism for handling events from the user interface and, with the
introduction of the JavaBeans specification and its incorporation in JDK
became possible for developers to create components that could be taken and reused
elsewhere more easily and could even be incorporated into graphical application
builder tools such as Microsoft Visual Basic or Borland JBuilder. But still, there
remained the issue of user interface sophistication. Notwithstanding the breadth of
JDK 1.1 and the immense improvement in the quality of the AWT, only two new
components (a scrolling window and a pop-up menu) were added. Developers still
had to produce most of their own user interface controls.
Enter Netscape
Meanwhile developers at Netscape had begun development of a set of improved user
controls that were eventually released under the banner of the Internet Foundation
What Are the Java Foundation Classes? 7
Classes (IFC). Implemented on top of AWT 1.0, the IFC components were a more
complete set than their predecessors and included some nice features such as dialogs
to allow a user to choose colors, files and fonts visually, buttons with images, sliders,
bitmaps (images that load synchronously), support for animation sequences and an
improved application framework that supported the nesting of parts of an application’s
interface inside other parts, with drawn boundaries to emphasize the nesting
and grouping of components.
As well as providing improved functionality, the IFC components were different
from AWT in another way. Whereas the AWT components are implemented partly in
Java and partly as a platform-dependent native library, the IFC components are written
entirely in Java. As a result, the IFC is immediately portable to any platform that
supports Java and takes with it its look-and-feel, unlike the AWT, which adopts the
appearance of its host platform.
The Swing Set: A Joint Effort
In the early part of 1997, developers from Netscape and JavaSoft began cooperating
on a project that was dubbed Swing. The aim of this project was to bring together the
AWT and the best parts of Netscape’s IFC to produce a fully-featured, robust set of
user interface classes to be released as part of JDK 1.2. Like their IFC counterparts,
these components (referred to as the Swing component set) would be written entirely
in Java to ensure portability and would, at some future time, allow JavaSoft to drop
most of the peer model that the AWT had used for its first two releases. The peer
model allowed JavaSoft to leverage native platform user interface support to get the
first release of the JDK into the hands of developers very quickly. This approach had,
of course, been extremely successful but was also, in part, responsible for the bad
reputation of the AWT on the Windows platform.
The Swing project would soon grow far beyond a straight merge of the IFC components
into the AWT, however. JavaSoft launched a 100% Pure Java initiative, of
which the Swing component set was, of course, a very good example and the number
of components to be included in this set increased. The final result of this project
was, as you’ll see, a comprehensive new set of controls that matches the best of the
competition and puts Java applications on a par with those written using the native
platform libraries. Whereas the Java developer had been forced to limit the scope of
the user interface or implement custom components, now the problem is more the
wealth of choice available.
Swing Plus More:The Java Foundation Classes
The Swing components are, without doubt, the most notable part of what JavaSoft
named the Java Foundation Classes, but they are not the whole story. Several
improvements that had gone into AWT 1.1, plus a few more enhancements planned
8 Chapter 1 Introduct ion to the Java Foundat ion Clas ses
for the JDK 1.2 time frame, were brought together and placed under the JFC banner.
In total, the JFC set consists of the following pieces:
· The Swing components
· The Desktop Colors feature of JDK 1.1
· The JDK 1.1 Printing facility
· The Java2D API, which supports enhanced text, color and image
support
· Accessibility, which provides support for technologies that make it
easier for users with disabilities to use the Java platform
· The JDK 1.1 cut-and-paste and clipboard facility, combined with a
new drag-and-drop facility introduced in JDK 1.2.
The full range of JFC features is delivered as part of JDK 1.2, but JavaSoft also
provides a package, called JFC 1.1 (also known as Swing 1.0), that contains the Swing
components in a form suitable for use with JDK 1.1. This package allows developers
to make use of the Swing components without having to wait for the final release of
JDK 1.2. This book focuses on the parts of the Java Foundation Classes that are
found in both the add-on package and in JDK 1.2. In addition, it covers the JDK 1.1
printing facility and a host of other features from JDK 1.1 that are closely related to
the Swing components, all of which must be properly understood in order for you to
make full use of Swing. This subset of the full JFC family consists mainly of the first
three items just listed.
What Is the Relationship between JFC and AWT?
With the introduction of a new set of components that use a different software architecture
from those already in the AWT, it is natural to wonder what will now become
of the AWT itself. As early as the last Beta version of JDK 1.1, JavaSoft were signaling
that there was a major change on the way for the AWT. In this last release before
final customer shipment, the AWT engineers introduced a new facility called
Lightweight Components. On the surface, this simply represented the ability to produce
a component or a container with a transparent background. This was done by
allowing developers to directly extend the Component and Container classes and
a nice example of a button with rounded edges was provided in the release documentation.
However, also among the documentation was a paper entitled AWT: The
Next Generation. This paper indicated that the future of the AWT lay in the direction
of lightweight components, enabling the removal of most of the troublesome peer
model. It also indicated that the peer model would be retained for a while for compatibility
reasons. This paper, of course, was the first public announcement of the initiative
that would eventually result in the delivery of the Swing components.
What Are the Java Foundation Classes? 9
In JFC 1.1 and JDK 1.2, the old AWT components and the Swing components are
both supported and the AWT components continue to use the peer model. It is not
clear how long this situation will continue. Some of the Swing components are actually
derived from an AWT component—for example, the JFrame component, which
provides the top-level window for an application, is derived from the AWT Frame
class. However, every AWT component has a corresponding Swing component, so it
is already possible to implement an application that does not directly depend on the
AWT, apart from the top-level window.