Example usage for java.awt EventQueue invokeAndWait

List of usage examples for java.awt EventQueue invokeAndWait

Introduction

In this page you can find the example usage for java.awt EventQueue invokeAndWait.

Prototype

public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException 

Source Link

Document

Causes runnable to have its run method called in the #isDispatchThread dispatch thread of Toolkit#getSystemEventQueue the system EventQueue .

Usage

From source file:Main.java

public static void invokeAndWait(final Runnable doRun) {
    if (doRun == null) {
        throw new IllegalArgumentException("The runnable cannot be null");
    }/*from ww w. j  a v  a  2  s  . c o  m*/
    if (EventQueue.isDispatchThread()) {
        doRun.run();
    } else {
        try {
            EventQueue.invokeAndWait(doRun);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Utility method used to run, synchronously, a Runnable on the main thread. Behaves exactly
 * like {@link EventQueue#invokeAndWait(Runnable)}, but can be called from the main thread as
 * well./*from  ww w.  j a va  2 s  .c  om*/
 * 
 * @param runnable
 * @throws InterruptedException
 * @throws InvocationTargetException
 */
public static void invokeAndWait(Runnable runnable) throws InvocationTargetException, InterruptedException {
    if (EventQueue.isDispatchThread()) {
        runnable.run();
    } else {
        EventQueue.invokeAndWait(runnable);
    }
}

From source file:Main.java

/**
 * Runs given runnable in dispatch thread.
 *//*from  w w w  . j  av  a 2  s.  c  om*/
public static void runInDispatchThread(final Runnable runnable) throws Exception {
    if (EventQueue.isDispatchThread()) {
        runnable.run();
    } else {
        EventQueue.invokeAndWait(runnable);
    }
}

From source file:Main.java

/**
 * Causes <code>runnable</code> to have its <code>run</code> method called
 * in the dispatch thread of {@link Toolkit#getSystemEventQueue the system
 * EventQueue} if this method is not being called from it. 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 the event
 * dispatcher thread./*w  w  w .  j  a  v a  2 s. c  o  m*/
 * 
 * @param runnable
 *            the <code>Runnable</code> whose <code>run</code> method should
 *            be executed synchronously on the <code>EventQueue</code>
 */
public static void invokeOnEdtAndWait(Runnable runnable) {
    boolean isEdt = EventQueue.isDispatchThread();
    if (!isEdt) {
        try {
            EventQueue.invokeAndWait(runnable);
        } catch (InterruptedException e) {
            System.err.println("EDT task interrupted");
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            System.err.println("EDT task invocation exception");
            e.printStackTrace();
        }
    } else {
        runnable.run();
    }
}

From source file:Main.java

/**
 * Causes runnable to have its run method called in the dispatch thread of
 * the event queue. This will happen after all pending events are processed.
 * The call blocks until this has happened.
 *///from ww w.j a v a 2s.  c  o  m
public static void invokeAndWait(final Runnable runnable) {
    if (EventQueue.isDispatchThread()) {
        runnable.run();
    } else {
        try {
            EventQueue.invokeAndWait(runnable);
        } catch (InterruptedException exception) {
            // Someone don't want to let us sleep. Go back to work.
        } catch (InvocationTargetException target) {
            final Throwable exception = target.getTargetException();
            if (exception instanceof RuntimeException) {
                throw (RuntimeException) exception;
            }
            if (exception instanceof Error) {
                throw (Error) exception;
            }
            // Should not happen, since {@link Runnable#run} do not allow checked exception.
            throw new UndeclaredThrowableException(exception, exception.getLocalizedMessage());
        }
    }
}

From source file:EventDispatcher.java

public static void fireEvent(final Dispatcher dispatcher, final List listeners, final Object evt,
        final boolean useEventQueue) {
    if (useEventQueue && !EventQueue.isDispatchThread()) {
        Runnable r = new Runnable() {
            public void run() {
                fireEvent(dispatcher, listeners, evt, useEventQueue);
            }//from w ww  .jav a2s  .  c  o  m
        };
        try {
            EventQueue.invokeAndWait(r);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // Assume they will get delivered????
            // be nice to wait on List but how???
        } catch (ThreadDeath td) {
            throw td;
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return;
    }

    Object[] ll = null;
    Throwable err = null;
    int retryCount = 10;
    while (--retryCount != 0) {
        // If the thread has been interrupted this can 'mess up'
        // the class loader and cause this otherwise safe code to
        // throw errors.
        try {
            synchronized (listeners) {
                if (listeners.size() == 0)
                    return;
                ll = listeners.toArray();
                break;
            }
        } catch (Throwable t) {
            err = t;
        }
    }
    if (ll == null) {
        if (err != null)
            err.printStackTrace();
        return;
    }
    dispatchEvent(dispatcher, ll, evt);
}

From source file:Main.java

/**
 * Invoke the specified <code>Runnable</code> on the AWT event dispatching thread now and wait
 * until completion.<br>//from  w w  w  .j a  v a  2  s.c o  m
 * Any exception is automatically caught by Icy exception handler, if you want to catch them use
 * {@link #invokeNow(Callable)} instead.<br>
 * Use this method carefully as it may lead to dead lock.
 */
public static void invokeNow(Runnable runnable) {
    if (isEventDispatchThread()) {
        try {
            runnable.run();
        } catch (Throwable t) {
            // the runnable thrown an exception
            //IcyExceptionHandler.handleException(t, true);
        }
    } else {
        try {
            EventQueue.invokeAndWait(runnable);
        } catch (InvocationTargetException e) {
            // the runnable thrown an exception
            //IcyExceptionHandler.handleException(e.getTargetException(), true);
        } catch (InterruptedException e) {
            // interrupt exception
            System.err.println("ThreadUtil.invokeNow(...) error :");
            //IcyExceptionHandler.showErrorMessage(e, true);
        }
    }
}

From source file:Main.java

/**
 * Invoke the specified <code>Callable</code> on the AWT event dispatching thread now and return
 * the result.<br>/* w  w w.j  a va  2s  .c  o  m*/
 * The returned result can be <code>null</code> when a {@link Throwable} exception happen.<br>
 * Use this method carefully as it may lead to dead lock.
 * 
 * @throws InterruptedException
 *         if the current thread was interrupted while waiting
 * @throws Exception
 *         if the computation threw an exception
 */
public static <T> T invokeNow(Callable<T> callable) throws InterruptedException, Exception {
    if (SwingUtilities.isEventDispatchThread())
        return callable.call();

    final FutureTask<T> task = new FutureTask<T>(callable);

    try {
        EventQueue.invokeAndWait(task);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof Exception)
            throw (Exception) e.getCause();

        // not an exception --> handle it
        //IcyExceptionHandler.showErrorMessage(e, true);
        return null;
    }

    try {
        return task.get();
    } catch (ExecutionException e) {
        if (e.getCause() instanceof Exception)
            throw (Exception) e.getCause();

        // not an exception --> handle it
        //IcyExceptionHandler.showErrorMessage(e, true);
        return null;
    }
}

From source file:org.parosproxy.paros.extension.trap.ProxyListenerTrap.java

private void setTrapDisplay(final HttpMessage msg, boolean isRequest) {
    setHttpDisplay(getTrapPanel(), msg, isRequest);
    try {/*  w  w  w.  j  a  v a 2 s.c o m*/
        EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                View.getSingleton().getMainFrame().toFront();
            }
        });
    } catch (Exception e) {
    }
}

From source file:org.kineticsystem.commons.data.model.swing.BasicList.java

/**
 * Notify all registered listeners with the given event. The event is fired
 * directly inside the AWT event-dispatching thread.
 * @param event The event containing information about changes occurred
 *     inside the list.//  w w w .  ja v  a 2  s.  com
 */
@Override
public void fireContentsChanged(ActiveListEvent event) {

    if (EventQueue.isDispatchThread()) {

        /*
         * Flag the event as "0=last" for compatibility with GroupingProxy
         * and SortingProxy classes.
         */

        event = (ActiveListEvent) event.clone();
        event.setSequenceNumber(0);

        super.fireContentsChanged(event);

    } else {
        Launcher runner = new Launcher(event);
        try {
            EventQueue.invokeAndWait(runner); // Wait until completion.
        } catch (InterruptedException ex) {
            logger.warn(ex);
        } catch (InvocationTargetException ex) {
            logger.error(ex);
        }
    }
}