Java Reference
In-Depth Information
CHAPTER 2
■ ■ ■
Event Handling with the Swing
Component Set
C hapter 1 provided a brief overview of the Swing component set. In this chapter, you will
start to look at the details of one aspect of using Swing components: event handling. When
working with the Swing component set, the delegation-based event-handling mechanism is
available, but you can also take advantage of several additional ways to respond to user-initiated
actions (as well as to programmatic events). In this chapter, you'll explore all these event-handling
response mechanisms. You'll also learn how Swing manages input focus and some techniques
for controlling how focus is handled.
As you explore event-handling capabilities, you will start to look at some actual Swing
components. In this chapter, you will be using the components in the simplest manner possible.
Feel free to first read up on the components covered in later chapters of this topic, and then
come back to this chapter for a general discussion of event handling. The later chapters of this
topic also contain specific details on event handling for each component.
Delegation-Based Event Handling
Sun Microsystems introduced the delegation-based event-handling mechanism into the Java
libraries with the release of JDK 1.1 and JavaBeans. Although the Java 1.0 libraries included the
Observer - Observable pair of objects that followed the Observer behavioral design pattern, this
wasn't an adequate long-term solution for user-interface programming. (The Java 1.0 contain-
ment event-handling mechanism was even worse.)
Event Delegation Model
The delegation-based event-handling mechanism is a specialized form of the Observer design
pattern. The Observer pattern is used when an Observer wants to know when a watched
object's state changes and what that state change is. In the case of the delegation-based event-
handling mechanism, instead of the Observer listening for a state change, the Observer listens
for events to happen.
17
Search WWH ::




Custom Search