Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Event Handling
-
Event handling is fundamental to the study of Graphical User Interfaces
(GUIs).
-
In Java, it's another one of those things that is harder than it should
be... it has changed quite a bit in different versions.
-
So what is an event?
-
Key press from the keyboard.
-
Mouse click, mouse move, etc.
-
Timers.
-
And more.
-
Event Sources are objects that transmit events, such as graphical
buttons.
-
Event Listeners are objects designed to act on events. Obviously,
you need both of these components to make an event really work.
-
You register the event listener objects with the event sources by sending
the event listener to a method on the event source.
-
Then, when the event occurs, the event source sends an Event object to
the event listener. The event object extends java.util.EventObject,
or some subclass of that. The event listener must extend an interface,
called a listener interface, with a method that the event source
knows to call when the event occurs. Remember polymorphism?
-
You can register more than one event listener with an event source.
Just call the method on the event source with each event listener.
-
The event listener uses the Event object passed to it from the event source
to find information about the event, and act on it if necessary.
Created by: Brandan
Jones January 4, 2002