Java Swing UI Thread Event invokeAndWaitSafely(final Runnable runnable)

Here you can find the source of invokeAndWaitSafely(final Runnable runnable)

Description

Will invoke the specified action in EDT in case it is called from non-EDT thread.

License

Open Source License

Parameter

Parameter Description
runnable runnable

Declaration

public static void invokeAndWaitSafely(final Runnable runnable) 

Method Source Code


//package com.java2s;
/*/*  w w w .  j  a  va2  s  .c  om*/
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library 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.
 *
 * WebLookAndFeel 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;

import java.lang.reflect.InvocationTargetException;

public class Main {
    /**
     * Will invoke the specified action in EDT in case it is called from non-EDT thread.
     * It will also block any exceptions thrown by "invokeAndWait" method.
     *
     * @param runnable runnable
     */
    public static void invokeAndWaitSafely(final Runnable runnable) {
        try {
            invokeAndWait(runnable);
        } catch (final Throwable e) {
            //
        }
    }

    /**
     * Will invoke the specified action in EDT in case it is called from non-EDT thread.
     *
     * @param runnable runnable
     * @throws InterruptedException
     * @throws java.lang.reflect.InvocationTargetException
     */
    public static void invokeAndWait(final Runnable runnable)
            throws InterruptedException, InvocationTargetException {
        if (SwingUtilities.isEventDispatchThread()) {
            runnable.run();
        } else {
            SwingUtilities.invokeAndWait(runnable);
        }
    }
}

Related

  1. invokeAndWait(final Runnable r)
  2. invokeAndWait(Runnable task)
  3. invokeAndWaitAsNeeded(Runnable r)
  4. invokeAndWaitEDT(final Runnable runnable)
  5. invokeAndWaitFromAnyThread(Runnable r)
  6. invokeAndWaitUnchecked(Runnable runnable)
  7. invokeEDT(final Runnable runnable)
  8. invokeInAWTThread(Runnable r)
  9. invokeInEventDispatchThread(@Nonnull Runnable runnable)