Class EventQueue in Java

0
85
Spread the love
  • 23
  • 34
  • 16
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    120
    Shares

Class EventQueue in Java

EventQueue class implements an event queue for AWT events in Java. When an EventQueue is created, a new thread is automatically created and started to remove events from the front of the queue and dispatch them to the appropriate component.

It is this thread, created by the EventQueue, that notifies event listeners and executes most of the code in a typical GUI – driven application.

The EventQueue class is a facility for queuing Java AWT events, either for the system or for some other purpose. We rarely need to create our own event queue; for most purposes, we will want to work with the system’s event queue, which you acquire using the Toolkit.

An application can create and use its own private EventQueue, but all AWT events are placed on and dispatched from a single system EventQueue. We can use the getSystemEventQueue() method of the Toolkit class to get the system EventQueue object.

getNextEvent() removes and returns the event at the front of the queue. It blocks if there are no events in the queue. peekEvent() returns the event at the front of the queue without removing it from the queue. Passed an optional AWTEvent id field, it returns the first event of the specified type.

Finally, postEvent() places a new event at the end of the event queue. Most applications do not need to use the EventQueue class at all; they can simply rely on the system to dispatch events automatically

The Class Structure of the EventQueue is given as

public class java.awt.EventQueue extends java.lang.Object{
// Public Constructor
public EventQueue();
// Public Instance Methods
public synchronized AWTEvent getNextEvent() throws InterruptedException:
public synchronized AWTEvent peekEvent();
public synchronized AWTEvent peekEvent(int id);
public synchronized void postEvent(AWTEvent theEvent);
}

The details of the class structure are given as follows:

public EventQueue();

public EventQueue() Creates an EventQueue for our own use.

public synchronized AWTEvent getNextEvent() throws InterruptedException;

public synchronized AWTEvent getNextEvent() throws InterruptedException method removes the next event from the event queue and returns it. If there are no events in the queue, this method will block until another thread posts one.

This method returns an AWTEvent taken from the event queue.

public synchronized AWTEvent peekEvent();

public synchronized AWTEvent peekEvent() method returns a reference to the next event on the queue without removing it from the queue.

This method returns the next AWTEvent on the event queue.

public synchronized AWTEvent peekEvent (int id);

public synchronized AWTEvent peekEvent(int id) method returns an event with the given type if one exists, but doesn’t remove the event from the queue.

This method returns an AWTEvent with the given type id; null if no event with the given type is currently in the queue.

Parameter
id – Type of event to find.

public synchronized void postEvent (AWTEvent theEvent);

public synchronized void postEvent(AWTEvent theEvent) method Posts a 1.1-style event to the EventQueue. If there is an existing event on the queue with the same ID and event source, the source Component’s coalesceEvents method will be called.

Parameter
theEvent -an instance of java.awt.AWTEvent, or a subclass of it

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 23
  • 34
  • 16
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    120
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here