Java tutorial
//package com.java2s; import java.lang.reflect.InvocationTargetException; import javax.swing.SwingUtilities; public class Main { /** * Safe handling for the {@link SwingUtilities#invokeAndWait(Runnable)} * method. It is unsafe to call invokeAndWait on the dispatch thread due to * deadlocks. This method simply runs the given {@link Runnable} if this is * called in the dispatch thread. * * @param r * - the runnable to run * @throws Exception * - any exceptions propagate, the possible * {@link InvocationTargetException} is unwrapped. */ public static void runOnDispatch(Runnable r) throws Exception { if (SwingUtilities.isEventDispatchThread()) { r.run(); } else { try { SwingUtilities.invokeAndWait(r); } catch (InvocationTargetException iie) { if (iie.getCause() instanceof Exception) { throw (Exception) iie.getCause(); } else { throw iie; } } } } }