Java tutorial
//package com.java2s; import java.lang.reflect.InvocationTargetException; import javax.swing.SwingUtilities; public class Main { public static void executeRunnable(Runnable runnable) { executeRunnable(runnable, true); } public static void executeRunnable(Runnable runnable, boolean waits) { if (SwingUtilities.isEventDispatchThread()) { runnable.run(); } else { try { if (waits) { SwingUtilities.invokeAndWait(runnable); } else { SwingUtilities.invokeLater(runnable); } } catch (InterruptedException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } }