Java Swing UI Thread Event invokeOnEDT(final Object target, final String methodName, final Object... args)

Here you can find the source of invokeOnEDT(final Object target, final String methodName, final Object... args)

Description

Invokes the given method on the EDT.

License

Apache License

Parameter

Parameter Description
target Target object exposing the specified method.
methodName Method to invoke.
args Arguments to pass to the method.

Declaration

public static void invokeOnEDT(final Object target, final String methodName, final Object... args) 

Method Source Code

//package com.java2s;
/**//  w w w  .java  2  s  . c  om
 * A collection of small utilities for component management and reflection handling.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import javax.swing.*;

import java.lang.reflect.Method;
import java.util.Arrays;

public class Main {
    /**
     * Invokes the target runnable on the event dispatch thread. This is equivalent to SwingUtilities.invokeLater(...),
     * this method was just put here to have all the invoke-methods in one place.
     *
     * @param r Runnable to start.
     */
    public static void invokeOnEDT(final Runnable r) {
        SwingUtilities.invokeLater(r);
    }

    /**
     * Invokes the given method on the EDT.
     *
     * @param target     Target object exposing the specified method.
     * @param methodName Method to invoke.
     * @param args       Arguments to pass to the method.
     */
    public static void invokeOnEDT(final Object target, final String methodName, final Object... args) {
        final Class[] types = new Class[args.length];
        for (int i = 0; i < types.length; i++)
            types[i] = args[i].getClass();
        final Method m = getMethod(target, methodName, types);
        if (m == null)
            throw new RuntimeException("No such method: " + methodName + '(' + Arrays.toString(types)
                    + ") found for target" + "class " + target.getClass().getName());
        if (!m.isAccessible())
            m.setAccessible(true);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    m.invoke(target, args);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    /**
     * Returns a the specified method of the target object, or null if the method does not exist.  This is merely a
     * conveniance method to avoid reflection's exceptions.
     *
     * @param target        Target object to check
     * @param methodName    Name of the method being queried
     * @param argumentTypes Types of the method's arguments
     * @return Method instance if the method exists, false otherwise.
     */
    public static synchronized Method getMethod(final Object target, final String methodName,
            final Class... argumentTypes) {
        return getMethod(target.getClass(), methodName, argumentTypes);
    }

    /**
     * Attempts to find a method with the given name and argument types on the specified class. Other than Class's
     * getMethod(String, Class...) method, this method will also return protected and private methods.
     *
     * @param clazz         Class supposed to offer the method being searched.
     * @param methodName    Name of the method.
     * @param argumentTypes The arguments found in the method signature, if any.
     * @return a method with the given name and argument types on the specified class, if any.
     */
    public static synchronized Method getMethod(final Class clazz, final String methodName,
            final Class... argumentTypes) {
        try {
            return clazz.getMethod(methodName, argumentTypes);
        } catch (NoSuchMethodException e) {
            /* Ok, so there's no public method... */
        }

        Class runner = clazz;
        while (runner != null) {
            try {
                return runner.getDeclaredMethod(methodName, argumentTypes);
            } catch (NoSuchMethodException e) {
                /* No luck here either */
            }
            runner = runner.getSuperclass();
        }

        /* Still no luck, means there is no suitable method */
        return null;
    }
}

Related

  1. invokeLaterEDT(final Runnable runnable)
  2. invokeLaterIfNeeded(Runnable runnable)
  3. invokeLaterOnEDT(Runnable runnable)
  4. invokeNow(Runnable runnable)
  5. invokeNowOrLater(Runnable run)
  6. invokeOnEDT(final Runnable aRunnable)
  7. invokeOnEventDispatchThread(Runnable r)
  8. invokeOnEventDispatchThreadIfRequired(final Runnable runnable)
  9. invokeOnEventThread(Runnable r)