Java Swing UI Thread Event invoke(Runnable runnable)

Here you can find the source of invoke(Runnable runnable)

Description

invoke

License

Open Source License

Declaration

public static void invoke(Runnable runnable) 

Method Source Code


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

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import javax.swing.SwingUtilities;
import com.google.common.util.concurrent.SettableFuture;

public class Main {
    public static <T> T invoke(final Callable<T> callable) {
        final SettableFuture<T> future = SettableFuture.create();

        try {/*from  www  .j  av a2 s . c  om*/
            invoke(new Runnable() {
                public void run() {
                    try {
                        future.set(callable.call());
                    } catch (Throwable e) {
                        future.setException(e);
                    }
                }
            });

            return future.get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public static void invoke(Runnable runnable) {
        if (SwingUtilities.isEventDispatchThread()) {
            runnable.run();
        } else {
            try {
                SwingUtilities.invokeAndWait(runnable);
            } catch (InvocationTargetException | InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

Related

  1. executeInSwingThread(final Runnable code)
  2. executeRunnable(Runnable runnable)
  3. executeTask(final Runnable task, final boolean async)
  4. getInstalledThemes(LookAndFeel laf)
  5. invoke(Runnable r)
  6. invoke(Runnable runnable)
  7. invokeAfter(final Runnable execute, int after)
  8. invokeAndContiune(Runnable runnable)
  9. invokeAndWait(final Runnable r)