Java Utililty Methods Swing UI Thread Event

List of utility methods to do Swing UI Thread Event

Description

The list of methods to do Swing UI Thread Event are organized into topic(s).

Method

voidinvokeLaterEDT(final Runnable runnable)
Execute LATER the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT)
SwingUtilities.invokeLater(runnable);
voidinvokeLaterIfNeeded(Runnable runnable)
Run operation in the swing thread.
if (SwingUtilities.isEventDispatchThread())
    runnable.run();
else {
    SwingUtilities.invokeLater(runnable);
voidinvokeLaterOnEDT(Runnable runnable)
invoke Later On EDT
try {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {
        SwingUtilities.invokeLater(runnable);
} catch (Throwable e) {
    throw new RuntimeException(e);
...
voidinvokeNow(Runnable runnable)
Invoke the specified Runnable on the AWT event dispatching thread now and wait until completion.
Any exception is automatically caught by Icy exception handler, if you want to catch them use #invokeNow(Callable) instead.
Use this method carefully as it may lead to dead lock.
if (isEventDispatchThread()) {
    try {
        runnable.run();
    } catch (Throwable t) {
} else {
    try {
        EventQueue.invokeAndWait(runnable);
...
voidinvokeNowOrLater(Runnable run)
Invokes run immediately if this is the EDT; otherwise, the Runnable is invoked on the EDT using invokeLater .
if (SwingUtilities.isEventDispatchThread()) {
    run.run();
} else {
    SwingUtilities.invokeLater(run);
voidinvokeOnEDT(final Object target, final String methodName, final Object... args)
Invokes the given method on the EDT.
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())
...
voidinvokeOnEDT(final Runnable aRunnable)
Similar as to SwingUtilities#invokeLater(Runnable) , but does a check first if the current running thread is already the EDT.
if (SwingUtilities.isEventDispatchThread()) {
    aRunnable.run();
} else {
    SwingUtilities.invokeLater(aRunnable);
voidinvokeOnEventDispatchThread(Runnable r)
Executes the given runnable now, if the current thread is the swing event dispatch thread, or later on the EDT, if not.
if (SwingUtilities.isEventDispatchThread()) {
    r.run();
} else {
    SwingUtilities.invokeLater(r);
voidinvokeOnEventDispatchThreadIfRequired(final Runnable runnable)
invoke On Event Dispatch Thread If Required
if (SwingUtilities.isEventDispatchThread())
    runnable.run();
else
    SwingUtilities.invokeLater(runnable);
voidinvokeOnEventThread(Runnable r)
GUI operations should be performed only on the AWT event dispatching thread.
if (SwingUtilities.isEventDispatchThread()) {
    r.run();
} else {
    SwingUtilities.invokeAndWait(r);