Java tutorial
//package com.java2s; import java.awt.EventQueue; import java.lang.reflect.InvocationTargetException; public class Main { /** * 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. * * @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(); } } }