Java Swing UI Thread Event invokeAndWaitAsNeeded(Runnable r)

Here you can find the source of invokeAndWaitAsNeeded(Runnable r)

Description

Invokes the given runnable on the Swing UI dispatch thread, blocking until the runnable has completed.

License

Open Source License

Parameter

Parameter Description
r The runnable to execute on the dispatch thread.

Declaration

public static void invokeAndWaitAsNeeded(Runnable r) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import java.lang.reflect.InvocationTargetException;

public class Main {
    /**//  www.  ja  v a 2  s.  com
     * Invokes the given runnable on the Swing UI dispatch thread, blocking until the runnable has completed. If the
     * current thread is the dispatch thread, the runnable is simply executed. If the current thread is not the dispatch
     * thread, then the current thread is blocked until the dispatch thread has completed executing the runnable.
     *
     * @param r The runnable to execute on the dispatch thread.
     */
    public static void invokeAndWaitAsNeeded(Runnable r) {
        if (SwingUtilities.isEventDispatchThread()) {
            r.run();
        } else {
            try {
                SwingUtilities.invokeAndWait(r);
            } catch (InterruptedException | InvocationTargetException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }
}

Related

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