Java Swing UI Thread Event doOnEventThreadAndWait(Runnable task)

Here you can find the source of doOnEventThreadAndWait(Runnable task)

Description

Execute a task on the even dispatch thread and wait for it to finish.

License

Open Source License

Parameter

Parameter Description
task The task to execute.

Declaration

public static void doOnEventThreadAndWait(Runnable task) 

Method Source Code


//package com.java2s;
/*//from w w  w  .j  a  v  a2s . c  o m
 * WorldPainter, a graphical and interactive map generator for Minecraft.
 * Copyright ? 2011-2015  pepsoft.org, The Netherlands
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;
import java.lang.reflect.InvocationTargetException;

public class Main {
    /**
     * Execute a task on the even dispatch thread and wait for it to finish. The
     * task <em>may</em> be executed on a different thread, so it must be
     * thread-safe.
     *
     * @param task The task to execute.
     */
    public static void doOnEventThreadAndWait(Runnable task) {
        if (SwingUtilities.isEventDispatchThread()) {
            task.run();
        } else {
            try {
                SwingUtilities.invokeAndWait(task);
            } catch (InterruptedException e) {
                throw new RuntimeException(
                        "Thread interrupted while waiting for task to execute on event dispatch thread", e);
            } catch (InvocationTargetException e) {
                Throwable cause = e.getTargetException();
                throw new RuntimeException(
                        cause.getClass().getSimpleName() + " thrown by task on event dispatch thread", cause);
            }
        }
    }
}

Related

  1. dispatchToEDT(Runnable runnable)
  2. doInBackground(final Runnable r)
  3. doInBackground(final Runnable runnable)
  4. doLater(final long milliseconds, final Runnable doThis)
  5. doLaterOnEventThread(Runnable task)
  6. doSwing(Runnable r)
  7. edtExecutor()
  8. edtInvokeLater(Runnable block)
  9. edtSmartInvokeAndWait(Runnable block)