Java Reference
In-Depth Information
Chapter 14. Graphical User Interfaces
Introduction
Java has had windowing capabilities since its earliest days. The first version made public
was the Abstract Windowing Toolkit, or AWT. Because it used the native toolkit compon-
ents, AWT was relatively small and simple. It suffered somewhat from being a “least com-
mon denominator”; a feature could not be added unless it could be implemented on all major
platforms that Java supported. The second major implementation was the Swing classes, re-
leased in 1998 as part of the Java Foundation Classes. Swing is a full-function, professional-
quality GUI toolkit designed to enable almost any kind of client-side GUI-based interaction.
AWT lives inside, or rather underneath, Swing, and, for this reason, many programs begin by
importing both java.awt and javax.swing . An alternative approach is exemplified by
IBM's SWT (Standard Windowing Toolkit), which is a thin wrapper for direct access to the
underlying toolkit. SWT is used in building the Eclipse IDE discussed in Compiling, Run-
ning, and Testing with an IDE . It's possible to build new applications using SWT, but Swing
is more portable and more widely used.
This chapter presents a few elements of Java windowing for the developer whose main ex-
posure to Java has been on the server side. Most of the examples are shown using Swing,
rather than the obsolescent AWT components; SWT is not covered at all. I assume that you
have at least a basic understanding of what GUI components are, which ones should be used
where, and so on. I will refer to JButton , JList , and JFrame , to name a few, without saying
much more about their basics or functionality. This is not intended to be a complete tutorial;
the reader needing more background should refer to Java in a Nutshell or Head First Java .
For a very thorough presentation on all aspects of Swing, I recommend Java Swing by Marc
Loy, Bob Eckstein, Dave Wood, Jim Elliott, and Brian Cole (O'Reilly). At around 1,250
pages, it's not an overnight read, but it is comprehensive.
Java's event model has evolved over time, too. In the earliest releases, the writer of a win-
dowed application had to write a single large event-handling method to deal with button
presses from all the GUI controls in the window. This was adequate for small programs, but
it did not scale well. The early version of my JabaDex contacts application had one large
event-handler method that tried to figure out which of 50 or 60 GUI controls had caused an
Search WWH ::




Custom Search