Chapter 13. 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. AWT used the native toolkit components, so it was relatively small and simple. AWT suffered somewhat from being a “least common 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, released 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 on inside, or rather underneath, Swing, and for this reason many programs begin by importing both java.awt and javax.swing.

This chapter presents a few elements of Java windowing for the developer whose main exposure to Java has been on the server side. The examples are shown using Swing, rather than the obsolescent AWT components. For a slightly more detailed presentation, the reader is referred to Learning Java. For a very thorough presentation on all aspects of Swing, I recommend the O’Reilly book Java Swing, by Robert Eckstein, Marc Loy, and Dave Wood. At 1252 pages it’s not an overnight read. But it is comprehensive.

Java’s event model has evolved over time, too. In JDK 1.0, the writer of a windowed 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 simple for small programs, but did not scale well. My JabaDex application had one large event handler method that tried to figure out which of 50 or 60 GUI controls had caused an event, which was tedious and error prone. In JDK 1.1, the new delegation event model was introduced. In this model, events are given only to classes that request them, which is done by registering a listener. This is discussed in Section 13.5 and shown in Section 13.5. At the same time, the language was extended ever so slightly to include the notion of inner classes . An inner class is simply a class whose definition is contained inside the body of another class. We use examples of two types of inner classes here; for details on the half-dozen different categories of inner classes, the reader is referred to Java in a Nutshell.

For this chapter, I make the assumption 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. If this stuff is mysterious to you, consult a good book on GUI design, such as the Java Look and Feel Design Guidelines.

Most of the GUI construction techniques in this chapter can be done for you, in some cases more quickly, by an integrated development environment (IDE). I have always believed, however, that understanding what goes on inside the code should be a prerequisite for being allowed to use an IDE. Those who disagree may be inclined to skip this chapter, go press a few buttons, and have the computer do the work for them. But you should at least skim this chapter to see what’s going on, so you’ll know where to look when you need it later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.133.160