Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
We have events on the Queue.
Now what do we do?
(Slide added in 397)
-
The AWT has to tell the listeners of that event that an event has occurred.
How?
-
It fetches events from the event queue, likely via getNextEvent().
Again, this is encapsulated functionality, but we can make assumptions...
-
It calls the processEvent() method of the event source. This method
dispatches the event to the event listeners..
-
It invokes the listener method for that event.
-
Thus, the event queue adds a brief delay. But this is a good thing.
-
The event queue can combine events, especially frequenty occurring events,
or events that duplicate other events.
-
The job of the event source...
-
The event source, a JButton object, for instance, has a big role in this
process.
-
Its processEvent() method is in charge of taking the event from the JVM
and dispatching it to the listeners.
-
How does it know what the listeners are? It maintains an EventListenerList
object of listeners. Listeners are added via an addXxxListener()
method on the source. The source then adds the listener to its EventListenerList.
-
When processEvent() is called, it gets all necessary listeners from the
EventListenerList and sends them the Event object.
-
So what is a listener?
-
A listener is an object with a method that accepts Event objects.
This method is fired from the processEvent() method of the event source.
-
The bottom line is, this is where the logic goes when the event happens.
This is where you place any logic that you want to perform when the event
happens.
-
Often, all this listener does is call a method on the event source object
itself. Thus, listeners are often internal classes so that
they can directly call methods on the source.
-
You can have 0 - many listeners per event. You can even re-use listeners
on different sources.
-
Graphically speaking....
So what do I have to do?



Created by: Brandan
Jones January 4, 2002