Java tutorial
//package com.java2s; import java.lang.reflect.InvocationTargetException; import javax.swing.SwingUtilities; public class Main { /** * 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() * * @param runnable * @throws InvocationTargetException * @throws InterruptedException */ public static void runNowInEventThread(Runnable runnable) throws InterruptedException, InvocationTargetException { if (SwingUtilities.isEventDispatchThread()) { runnable.run(); } else { SwingUtilities.invokeAndWait(runnable); } } }