Java tutorial
//package com.java2s; import javax.swing.*; public class Main { public static void invokeAndWait(Runnable r) { if (SwingUtilities.isEventDispatchThread()) { r.run(); return; } try { SwingUtilities.invokeAndWait(r); } catch (Exception e) { throw new IllegalStateException("Unable to invoke/wait for task", e); } } }