Java Utililty Methods Swing UI Thread Event

List of utility methods to do Swing UI Thread Event

Description

The list of methods to do Swing UI Thread Event are organized into topic(s).

Method

voidinvokeAndWaitEDT(final Runnable runnable)
Execute the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT) And waits for completion
if (isEDT()) {
    runnable.run();
} else {
    if (Thread.currentThread().isInterrupted()) {
        invokeLaterEDT(runnable);
    } else {
        try {
            SwingUtilities.invokeAndWait(runnable);
...
voidinvokeAndWaitFromAnyThread(Runnable r)
In EDT just runs the runnable (so in that thread the pending AWT events are _not_ dispatched before running the runnable).
if (SwingUtilities.isEventDispatchThread()) {
    try {
        r.run();
    } catch (RuntimeException e) {
        throw new InvocationTargetException(e);
} else {
    SwingUtilities.invokeAndWait(r);
...
voidinvokeAndWaitSafely(final Runnable runnable)
Will invoke the specified action in EDT in case it is called from non-EDT thread.
try {
    invokeAndWait(runnable);
} catch (final Throwable e) {
voidinvokeAndWaitUnchecked(Runnable runnable)
SwingUtilities.invokeAndWait exceptions rethrown as unchecked RuntimeExceptions.
try {
    SwingUtilities.invokeAndWait(runnable);
} catch (Exception e) {
    throw new RuntimeException("Cannot wait for invokeAndWait", e);
voidinvokeEDT(final Runnable runnable)
Execute the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT)
if (isEDT()) {
    runnable.run();
} else {
    invokeLaterEDT(runnable);
voidinvokeInAWTThread(Runnable r)
invoke In AWT Thread
invokeInAWTThread(r, false);
voidinvokeInEventDispatchThread(@Nonnull Runnable runnable)
Invokes the runnable within the EDT
if (isEventDispatchThread()) {
    runnable.run();
} else {
    try {
        SwingUtilities.invokeAndWait(runnable);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
...
voidinvokeInSwingThread(Runnable r)
invoke In Swing Thread
if (EventQueue.isDispatchThread()) {
    r.run();
} else {
    try {
        SwingUtilities.invokeAndWait(r);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
voidinvokeLater(final Runnable r)
invoke Later
if (SwingUtilities.isEventDispatchThread()) {
    r.run();
} else {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            r.run();
    });
voidinvokeLater(Runnable runnable, boolean forceLater)
Invoke "runnable" on the AWT event dispatching thread.
If you are in the AWT event dispatching thread the runnable can be executed immediately
depending the value of forceLater parameter.
if (SwingUtilities.isEventDispatchThread() && !forceLater)
    runnable.run();
else
    SwingUtilities.invokeLater(runnable);