Working with the event queue

The java.awt.EventQueue class represents a queue of events waiting to be processed. You can create your own instances of this class using the noargs constructor:

public EventQueue()

For example,

EventQueue MyQueue = new EventQueue();

However, most of the time you'll be more interested in the system event queue. This is created for you automatically. You can get a reference to it with the getSystemEventQueue() method in the java.awt.Toolkit class like this:

EventQueue systemQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();

An applet cannot call this method without generating a security exception.

Once you have a reference to the system event queue, you can manipulate it with the following methods:

  public void     postEvent(AWTEvent evt)
  public AWTEvent getNextEvent()
  public AWTEvent peekEvent()
  public AWTEvent peekEvent(int n)

postEvent() lets you put an event in the queue. getNextEvent() removes and returns the top event in the queue. peekEvent() returns the top event in the queue but does not remove it from the queue. peekEvent(int n) returns the nth event in the queue.

Previous | Next | Top | Cafe au Lait

Copyright 1997, 2006 Elliotte Rusty Harold
Last Modified April 26, 2006