Java Swing UI Thread Event invokeAndWaitEDT(final Runnable runnable)

Here you can find the source of invokeAndWaitEDT(final Runnable runnable)

Description

Execute the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT) And waits for completion

License

Open Source License

Parameter

Parameter Description
runnable runnable code dedicated to Swing

Exception

Parameter Description
IllegalStateException if any exception occurs while the given runnable code executes using EDT

Declaration

public static void invokeAndWaitEDT(final Runnable runnable) throws IllegalStateException 

Method Source Code


//package com.java2s;
import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;

public class Main {
    /**/*from   w  w w .j a  va 2s.  c  o  m*/
     * Execute the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT)
     * And waits for completion
     * @param runnable runnable code dedicated to Swing
     * @throws IllegalStateException if any exception occurs while the given runnable code executes using EDT
     */
    public static void invokeAndWaitEDT(final Runnable runnable) throws IllegalStateException {
        if (isEDT()) {
            // current Thread is EDT, simply execute runnable:
            runnable.run();
        } else {
            // If the current thread is interrupted, then use invoke later EDT (i.e. do not wait):
            if (Thread.currentThread().isInterrupted()) {
                invokeLaterEDT(runnable);
            } else {
                try {
                    // Using invokeAndWait to be in sync with the calling thread:
                    SwingUtilities.invokeAndWait(runnable);

                } catch (InterruptedException ie) {
                    // propagate the exception because it should never happen:
                    throw new IllegalStateException(
                            "SwingUtils.invokeAndWaitEDT : interrupted while running " + runnable, ie);
                } catch (InvocationTargetException ite) {
                    // propagate the internal exception :
                    throw new IllegalStateException(
                            "SwingUtils.invokeAndWaitEDT : an exception occured while running " + runnable,
                            ite.getCause());
                }
            }
        }
    }

    /**
     * Returns true if the current thread is the Event Dispatcher Thread (EDT)
     *
     * @return true if the current thread is the Event Dispatcher Thread (EDT)
     */
    public static boolean isEDT() {
        return SwingUtilities.isEventDispatchThread();
    }

    /**
     * Execute LATER the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT)
     * @param runnable runnable code dedicated to Swing
     */
    public static void invokeLaterEDT(final Runnable runnable) {
        // current Thread is NOT EDT, simply invoke later runnable using EDT:
        SwingUtilities.invokeLater(runnable);
    }
}

Related

  1. invokeAfter(final Runnable execute, int after)
  2. invokeAndContiune(Runnable runnable)
  3. invokeAndWait(final Runnable r)
  4. invokeAndWait(Runnable task)
  5. invokeAndWaitAsNeeded(Runnable r)
  6. invokeAndWaitFromAnyThread(Runnable r)
  7. invokeAndWaitSafely(final Runnable runnable)
  8. invokeAndWaitUnchecked(Runnable runnable)
  9. invokeEDT(final Runnable runnable)