Example usage for javax.swing SwingUtilities isEventDispatchThread

List of usage examples for javax.swing SwingUtilities isEventDispatchThread

Introduction

In this page you can find the example usage for javax.swing SwingUtilities isEventDispatchThread.

Prototype

public static boolean isEventDispatchThread() 

Source Link

Document

Returns true if the current thread is an AWT event dispatching thread.

Usage

From source file:Main.java

/**
 * Checks whether the current thread is Swing EDT. If not - throws an Exception with an advice
 *
 * @throws IllegalStateException when the current thread is not Swing EDT
 *//*from  www  .  j a va 2s . c o  m*/
public static void checkForEventDispatchThread() throws IllegalStateException {
    Preconditions.checkState(SwingUtilities.isEventDispatchThread(),
            "The method that caused this trace must be called in the Event Dispatch Thread, see http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html");
}

From source file:Main.java

/**
 * Executes a runnable piece of code, altering GUI components, which should be done on the EDT. If
 * the current thread is the EDT, the code is executed normally, otherwise it is wrapped in a
 * SwingUtilities.invokeLater(...)./*from  w  ww . j a v  a  2s  . com*/
 * @param runnable The runnable to be executed on the EDT
 */
public static void executeWithEDTCheck(Runnable runnable) {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {
        SwingUtilities.invokeLater(runnable);
    }
}

From source file:Main.java

/**
 * Assert we are in the event dispatching thread.
 *//*from   w w  w.j a  v a  2  s.  co  m*/
public static void assertEventDispatcherThread() {
    if (!SwingUtilities.isEventDispatchThread())
        throw new IllegalStateException("Not an AWT thread.");
}

From source file:Main.java

/**
 * Runs the given Runnable inside the Swing event dispatch thread and blocks
 * until the runnable has completed. If the current thread is the Swing EDT
 * then it just runs it, otherwise it calls SwingUtilities.invokeLater()
 * //w  w  w  .  j  a  v  a  2 s  . c om
 * @param runnable
 * @throws InvocationTargetException
 * @throws InterruptedException
 */
public static void runNowInEventThread(Runnable runnable)
        throws InterruptedException, InvocationTargetException {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {
        SwingUtilities.invokeAndWait(runnable);
    }
}

From source file:Main.java

public static void invokeLaterOnEDT(Runnable runnable) {
    try {//  ww  w . j a  va2  s  . c  o  m
        if (SwingUtilities.isEventDispatchThread()) {
            runnable.run();
        } else {
            SwingUtilities.invokeLater(runnable);
        }
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void runInSwingThread(final Runnable runnable) {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();/*from ww  w.j a va 2  s  . c om*/
    } else {
        try {
            SwingUtilities.invokeAndWait(runnable);
        } catch (final InvocationTargetException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:Main.java

/**
 * The runnable is responsible for leaving the nested event loop.
 *//*from  w  w w .  j  a  v  a  2s.c  o m*/
static void runOnEDTAndWait(Object nestedLoopKey, Runnable r) {
    Toolkit.getToolkit().checkFxUserThread();

    if (SwingUtilities.isEventDispatchThread()) {
        r.run();
    } else {
        eventLoopKeys.add(nestedLoopKey);
        SwingUtilities.invokeLater(r);
        Toolkit.getToolkit().enterNestedEventLoop(nestedLoopKey);
    }
}

From source file:Main.java

/**
 * A wrapper around SwingUtilities.invokeAndWait() that makes sure that 
 * SwingUtilities.invokeAndWait() is only called when the current thread is 
 * not the AWT event dispatching thread, as required by the documentation
 * of SwingUtilities.invokeAndWait(); plus catches exceptions thrown by 
 * SwingUtilities.invokeAndWait().//from w w w  .j  a v  a2 s .  co m
 * @param runnable The Runnable to call in the event dispatch thread.
 */
public static void invokeAndWait(Runnable runnable) {
    try {
        if (SwingUtilities.isEventDispatchThread())
            runnable.run();
        else
            SwingUtilities.invokeAndWait(runnable);
    } catch (InvocationTargetException ex) {
        ex.printStackTrace();
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * A wrapper around SwingUtilities.invokeAndWait() that makes sure that 
 * SwingUtilities.invokeAndWait() is only called when the current thread is 
 * not the AWT event dispatching thread, as required by the documentation
 * of SwingUtilities.invokeAndWait(); plus catches exceptions thrown by 
 * SwingUtilities.invokeAndWait().//from ww  w  .j  a  v a  2  s  . c o  m
 * @param runnable The Runnable to call in the event dispatch thread.
 */
public static void invokeAndWait(Runnable runnable) {
    try {
        if (SwingUtilities.isEventDispatchThread())
            runnable.run();
        else
            SwingUtilities.invokeAndWait(runnable);
    } catch (InvocationTargetException | InterruptedException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * In EDT just runs the runnable (so in that thread the pending AWT events
 * are _not_ dispatched before running the runnable).
 *///from ww w. j  av a  2 s. c om
public static void invokeAndWaitFromAnyThread(Runnable r)
        throws InterruptedException, InvocationTargetException {
    if (SwingUtilities.isEventDispatchThread()) {
        try {
            r.run();
        } catch (RuntimeException e) {
            throw new InvocationTargetException(e);
        }
    } else {
        SwingUtilities.invokeAndWait(r);
    }
}