EventQueue (Java SE 10 & JDK 10 )
- java.lang.Object
-
- java.awt.EventQueue
-
public class EventQueue extends Object
EventQueueis a platform-independent class that queues events, both from the underlying peer classes and from trusted application classes.It encapsulates asynchronous event dispatch machinery which extracts events from the queue and dispatches them by calling
dispatchEvent(AWTEvent)method on thisEventQueuewith the event to be dispatched as an argument. The particular behavior of this machinery is implementation-dependent. The only requirements are that events which were actually enqueued to this queue (note that events being posted to theEventQueuecan be coalesced) are dispatched:- Sequentially.
- That is, it is not permitted that several events from this queue are dispatched simultaneously.
- In the same order as they are enqueued.
- That is, if
AWTEventA is enqueued to theEventQueuebeforeAWTEventB then event B will not be dispatched before event A.
Some browsers partition applets in different code bases into separate contexts, and establish walls between these contexts. In such a scenario, there will be one
EventQueueper context. Other browsers place all applets into the same context, implying that there will be only a single, globalEventQueuefor all applets. This behavior is implementation-dependent. Consult your browser's documentation for more information.For information on the threading issues of the event dispatch machinery, see AWT Threading Issues.
- Since:
- 1.1
-
-
Constructor Summary
Constructors Constructor Description EventQueue()Initializes a new instance of
EventQueue.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description SecondaryLoopcreateSecondaryLoop()Creates a new
secondary loopassociated with this event queue.protected voiddispatchEvent(AWTEvent event)Dispatches an event.
static AWTEventgetCurrentEvent()Returns the event currently being dispatched by the
EventQueueassociated with the calling thread.static longgetMostRecentEventTime()Returns the timestamp of the most recent event that had a timestamp, and that was dispatched from the
EventQueueassociated with the calling thread.AWTEventgetNextEvent()Removes an event from the
EventQueueand returns it.static voidinvokeAndWait(Runnable runnable)Causes
runnableto have itsrunmethod called in thedispatch threadofthe system EventQueue.static voidinvokeLater(Runnable runnable)Causes
runnableto have itsrunmethod called in thedispatch threadofthe system EventQueue.static booleanisDispatchThread()Returns true if the calling thread is
the current AWT EventQueue's dispatch thread.AWTEventpeekEvent()Returns the first event on the
EventQueuewithout removing it.AWTEventpeekEvent(int id)Returns the first event with the specified id, if any.
protected voidpop()Stops dispatching events using this
EventQueue.voidpostEvent(AWTEvent theEvent)Posts a 1.1-style event to the
EventQueue.voidpush(EventQueue newEventQueue)Replaces the existing
EventQueuewith the specified one.
-
-
-
Method Detail
-
postEvent
public void postEvent(AWTEvent theEvent)
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 sourceComponent'scoalesceEventsmethod will be called.- Parameters:
theEvent- an instance ofjava.awt.AWTEvent, or a subclass of it- Throws:
NullPointerException- iftheEventisnull
-
getNextEvent
public AWTEvent getNextEvent() throws InterruptedException
Removes an event from the
EventQueueand returns it. This method will block until an event has been posted by another thread.- Returns:
- the next
AWTEvent - Throws:
InterruptedException- if any thread has interrupted this thread
-
peekEvent
public AWTEvent peekEvent()
Returns the first event on the
EventQueuewithout removing it.- Returns:
- the first event
-
peekEvent
public AWTEvent peekEvent(int id)
Returns the first event with the specified id, if any.
- Parameters:
id- the id of the type of event desired- Returns:
- the first event of the specified id or
nullif there is no such event
-
dispatchEvent
protected void dispatchEvent(AWTEvent event)
Dispatches an event. The manner in which the event is dispatched depends upon the type of the event and the type of the event's source object:
Event types, source types, and dispatch methods Event Type Source Type Dispatched To ActiveEvent Any event.dispatch() Other Component source.dispatchEvent(AWTEvent) Other MenuComponent source.dispatchEvent(AWTEvent) Other Other No action (ignored) - Parameters:
event- an instance ofjava.awt.AWTEvent, or a subclass of it- Throws:
NullPointerException- ifeventisnull- Since:
- 1.2
-
getMostRecentEventTime
public static long getMostRecentEventTime()
Returns the timestamp of the most recent event that had a timestamp, and that was dispatched from the
EventQueueassociated with the calling thread. If an event with a timestamp is currently being dispatched, its timestamp will be returned. If no events have yet been dispatched, the EventQueue's initialization time will be returned instead.In the current version of the JDK, onlyInputEvents,ActionEvents, andInvocationEvents have timestamps; however, future versions of the JDK may add timestamps to additional event types. Note that this method should only be invoked from an application'sevent dispatching thread. If this method is invoked from another thread, the current system time (as reported bySystem.currentTimeMillis()) will be returned instead.- Returns:
- the timestamp of the last
InputEvent,ActionEvent, orInvocationEventto be dispatched, orSystem.currentTimeMillis()if this method is invoked on a thread other than an event dispatching thread - Since:
- 1.4
- See Also:
InputEvent.getWhen(),ActionEvent.getWhen(),InvocationEvent.getWhen(),isDispatchThread()
-
getCurrentEvent
public static AWTEvent getCurrentEvent()
Returns the event currently being dispatched by the
EventQueueassociated with the calling thread. This is useful if a method needs access to the event, but was not designed to receive a reference to it as an argument. Note that this method should only be invoked from an application's event dispatching thread. If this method is invoked from another thread, null will be returned.- Returns:
- the event currently being dispatched, or null if this method is invoked on a thread other than an event dispatching thread
- Since:
- 1.4
-
push
public void push(EventQueue newEventQueue)
Replaces the existing
EventQueuewith the specified one. Any pending events are transferred to the newEventQueuefor processing by it.- Parameters:
newEventQueue- anEventQueue(or subclass thereof) instance to be use- Throws:
NullPointerException- ifnewEventQueueisnull- Since:
- 1.2
- See Also:
pop()
-
pop
protected void pop() throws EmptyStackExceptionStops dispatching events using this
EventQueue. Any pending events are transferred to the previousEventQueuefor processing.Warning: To avoid deadlock, do not declare this method synchronized in a subclass.
- Throws:
EmptyStackException- if no previous push was made on thisEventQueue- Since:
- 1.2
- See Also:
push(java.awt.EventQueue)
-
createSecondaryLoop
public SecondaryLoop createSecondaryLoop()
Creates a new
secondary loopassociated with this event queue. Use theSecondaryLoop.enter()andSecondaryLoop.exit()methods to start and stop the event loop and dispatch the events from this queue.- Returns:
- secondaryLoop A new secondary loop object, which can be used to launch a new nested event loop and dispatch events from this queue
- Since:
- 1.7
- See Also:
SecondaryLoop.enter(),SecondaryLoop.exit()
-
isDispatchThread
public static boolean isDispatchThread()
- Returns:
- true if running in
the current AWT EventQueue's dispatch thread - Since:
- 1.2
- See Also:
invokeLater(java.lang.Runnable),invokeAndWait(java.lang.Runnable),Toolkit.getSystemEventQueue()
-
invokeLater
public static void invokeLater(Runnable runnable)
Causes
runnableto have itsrunmethod called in thedispatch threadofthe system EventQueue. This will happen after all pending events are processed.- Parameters:
runnable- theRunnablewhoserunmethod should be executed asynchronously in theevent dispatch threadofthe system EventQueue- Since:
- 1.2
- See Also:
invokeAndWait(java.lang.Runnable),Toolkit.getSystemEventQueue(),isDispatchThread()
-
invokeAndWait
public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException
Causes
runnableto have itsrunmethod called in thedispatch threadofthe system EventQueue. This will happen after all pending events are processed. The call blocks until this has happened. This method will throw an Error if called from theevent dispatcher thread.- Parameters:
runnable- theRunnablewhoserunmethod should be executed synchronously in theevent dispatch threadofthe system EventQueue- Throws:
InterruptedException- if any thread has interrupted this threadInvocationTargetException- if an throwable is thrown when runningrunnable- Since:
- 1.2
- See Also:
invokeLater(java.lang.Runnable),Toolkit.getSystemEventQueue(),isDispatchThread()
-
-
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2018, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.