Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Event Queue
(This slide added in 397.)
-
When an event occurs, the operating system notifies the Java VM.
-
The VM takes the information from the OS and creates an event.
-
Specifically, it constructs an AWTEvent object.
-
When constructing an AWTEvent, it must pass the source object (the event
source) into the constructor. This tells us a lot - it tells us what
object caused the event to happen (a specific JButton or JRadioButton,
for example), and it can guide the JVM to find the listeners, as discussed
below.
-
The event is put on something called an event queue. The event
queue is actually an object, java.awt.EventQueue.
-
So, events that magically appear in your program are actually constructed
internally by the JVM and thrown on the EventQueue.
-
We don't need to know how the EventQueue works internally, that is the
point of encapsulation. But, for academic reasons, we can
speculate.
-
The EventQueue must hold multiple AWTEvent objects. Thus, we assume
it implements a List
object of some type, perhaps a Vector.
-
Then, it must be able to add and remove elements from that List.
-
We rarely need to manipulate the EventQueue manually - but we can if we
want to. Here are some useful methods of the EventQueue:
-
AWTEvent peekEvent() allows us to see what event is next in the
queue without actually removing it.
-
AWTEvent getNextEvent() gets the next event and removes it from
the EventQueue.
-
void postEvent(AWTEvent anEvent) adds anEvent to the EventQueue.
Now What Do We Do?



Created by: Brandan
Jones January 4, 2002