Java tutorial
//package com.java2s; /* * GeoTools - The Open Source Java GIS Toolkit * http://geotools.org * * (C) 2001-2008, Open Source Geospatial Foundation (OSGeo) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import java.awt.EventQueue; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.UndeclaredThrowableException; public class Main { /** * Causes runnable to have its run method called in the dispatch thread of * the event queue. This will happen after all pending events are processed. * The call blocks until this has happened. */ public static void invokeAndWait(final Runnable runnable) { if (EventQueue.isDispatchThread()) { runnable.run(); } else { try { EventQueue.invokeAndWait(runnable); } catch (InterruptedException exception) { // Someone don't want to let us sleep. Go back to work. } catch (InvocationTargetException target) { final Throwable exception = target.getTargetException(); if (exception instanceof RuntimeException) { throw (RuntimeException) exception; } if (exception instanceof Error) { throw (Error) exception; } // Should not happen, since {@link Runnable#run} do not allow checked exception. throw new UndeclaredThrowableException(exception, exception.getLocalizedMessage()); } } } }