Programming and Problem Solving with Java

(やまだぃちぅ) #1
8.3 Event Handling | 385

8.3 Event Handling


Recall from Chapter 1 that one of Java’s control structures is asynchronous control. A mouse
click is an action that can trigger the application to perform a set of instructions at any time
during its execution. For this reason, it is calledasynchronous, which means “not
connected with a specific moment in time.” The application can be busily work-
ing away on some operation, when the user chooses that random moment to
click the mouse. The application must stop what it is doing and transfer control
to the instructions that take care of the mouse click. Once it has responded to the
user’s action, control returns to the application at the point where it was inter-
rupted. In Java, a user action of this form is called anevent, and responding to it
is calledevent handling. Events are handled by special methods called event handlers.
The event-handling process sounds very much like calling a method, but
there is a basic difference. We write a statement in our code that explicitly calls
a method at a specific point in the application’s execution. An event handler, on
the other hand, can implicitly be called at any time, and no corresponding method
call statement appears anywhere in the application. How, then, is the event han-
dler called?
The answer is that there are objects called event listenerswhose function is to
watch for (that is, listen for) events to occur and respond to them. An object that
generates a particular kind of event is called an event source. When an event source
generates an event, we say that the source fired the event. Every event source keeps
a list of the listeners that want to be notified when that kind of event occurs.
Letting an event source know that the listener wants to be notified is called reg-
istering the listener. Perhaps you’ve filled out an online software registration form
that contained a box to check beside words that said something like, “Send me
e-mail notification of new products and product updates.” Filling out this form and
submitting it is like registering a listener (you) with an event source (the company).
When the company has an update to your software (an event fires), it sends you
e-mail (notifies the listener).
Each listener has an event handler method that is designed to respond to
(handle) the event. When an event occurs, the appropriate listener is notified by
calling its event handler method. When the event handler returns (the method fin-
ishes executing), the application continues executing. Figure 8.4 illustrates the
process for a buttonthat the user can click in a frame. If no listener is interested
or if the interested listener doesn’t have an event handler for that particular type
of event, the event is ignored and the application continues uninterrupted.


Creating a Button


Let’s make the abstract concept of event handling more concrete by introducing the JButton
class that allows us to put a button on the screen. We can then explore how to handle the
events that it generates. Before we can handle a button event, we have to put a button object


Asynchronous Not occurring
at the same moment in time as
some specific operation of the
computer; not synchronized
with the computer’s actions
Event An action, such as a
mouse click, that takes place
asynchronously with respect to
the execution of the application
Event handling The process of
responding to events
Event handler A method that
responds to an event; part of the
event listener that is invoked by
the event source object
Event listener An object that
contains event handler methods
Event source An object that
generates events
Firing an event An event
source generates an event
Registering a listener Adding
a listener to an event source’s list
of interested listeners
Button A component that can
be added to a frame and that
fires an event (called a button
event) when the user clicks it
with the mouse
Free download pdf