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

voidassertEventDispatcherThread()
Assert we are in the event dispatching thread.
if (!SwingUtilities.isEventDispatchThread())
    throw new IllegalStateException("Not an AWT thread.");
voidassertIsEDT()
assert Is EDT
if (!SwingUtilities.isEventDispatchThread()) {
    throw new RuntimeException("Not in the event dispatch thread");
voidassertNotEventDispatchThread()

assertNotEventDispatchThread

if (isEventDispatchThread()) {
    throw new IllegalThreadStateException("Is EDT");
voidcallOnGUIThread(Runnable runnable)
call On GUI Thread
Preconditions.checkNotNull(runnable);
if (SwingUtilities.isEventDispatchThread()) {
    runnable.run();
} else {
    SwingUtilities.invokeLater(runnable::run);
voidcheckForEventDispatchThread()
Checks whether the current thread is Swing EDT.
Preconditions.checkState(SwingUtilities.isEventDispatchThread(),
        "The method that caused this trace must be called in the Event Dispatch Thread, see http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html");
voiddispatchOnAWTThreadLater(Runnable r)
dispatch On AWT Thread Later
if (SwingUtilities.isEventDispatchThread()) {
    r.run();
} else {
    SwingUtilities.invokeLater(r);
voiddispatchOnAWTThreadNow(Runnable r)
dispatch On AWT Thread Now
if (SwingUtilities.isEventDispatchThread())
    r.run();
else
    try {
        SwingUtilities.invokeAndWait(r);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
...
voiddispatchToEDT(Runnable runnable)
dispatch To EDT
if (!SwingUtilities.isEventDispatchThread()) {
    SwingUtilities.invokeLater(runnable);
} else {
    runnable.run();
voiddoInBackground(final Runnable r)
do In Background
new SwingWorker<Void, Void>() {
    @Override
    protected Void doInBackground() {
        r.run();
        return null;
}.execute();
SwingWorkerdoInBackground(final Runnable runnable)
Launches a SwingWorker , executing the provided Runnable in the SwingWorker#doInBackground() method.
SwingWorker result;
result = new SwingWorker() {
    @Override
    protected Object doInBackground() throws Exception {
        runnable.run();
        return null;
};
...