Blakes 21 Days Chapter 12 Document

Day 12, Responding to User Input

Event Listeners

Setting Up Components

Event-Handling Methods

First Program

Listing 12.1 The Full Text of

The Explanation

Figure 12.1 - The TitleCar application. - goes here

Working with Methods

Action Events

Focus Events

Second Program

Listing 12.2 The Full Text of

The Explanation

Figure 12.2 - The Calculator application - goes here

Item Events

Third Program

Listing 12.3 The Full Text of

The Explanation

Figure 12.3 - The output of the FormatChooser application - goes here

Key Events

Mouse Events

Mouse Motion Events

Fourth Program

Listing 12.4 The Full Text of

The Explanation

Figure 12.4 - The running MousePrank application - goes here

Window Events

Using Adapter Classes

Fifth Program

Listing 12.5 The Full Text of

The Explanation

Figure 12.5 - The running KeyChecker application - goes here !!

Using Inner Classes

Sixth Program

Listing 12.6 The Full Text of

The Explanation

A note about the First Program - TitleBar


Q & A

Quiz - Questions

  1. If you use this in a method call such as addActionListener(this), what object is being registered as a listener ?
    1. An adapter class
    2. The current class
    3. no class
  2. What is the benefit of subclassing an adapter class such as WindowAdapter (which implements the WindowListener interface) ?
    1. You inherit all the behavior of that class.
    2. The subclass automatically becomes a listener.
    3. You don't need to implement any WindowListener methods you won't be using.
  3. What kind of event is generated when you press Tab to leave a text field ?
    1. FocusEvent
    2. WindowEvent
    3. ActionEvent


  1. B. The current class must implement the correct listener interface and the required methods.
  2. C. Because most listener interfaces contain more methods than you need, using an adapter class as a superclass saves the hassle of implementing empty methods just to implement the interface.
  3. A. A user interface component loses focus when the user stops editing that component and moves to a different part of the interface.

Certification Practice