Example usage for java.awt EventQueue isDispatchThread

List of usage examples for java.awt EventQueue isDispatchThread

Introduction

In this page you can find the example usage for java.awt EventQueue isDispatchThread.

Prototype

public static boolean isDispatchThread() 

Source Link

Document

Returns true if the calling thread is Toolkit#getSystemEventQueue the current AWT EventQueue 's dispatch thread.

Usage

From source file:Main.java

public static void invokeAndWait(final Runnable doRun) {
    if (doRun == null) {
        throw new IllegalArgumentException("The runnable cannot be null");
    }//from  www .j  av  a 2 s. co  m
    if (EventQueue.isDispatchThread()) {
        doRun.run();
    } else {
        try {
            EventQueue.invokeAndWait(doRun);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Print a stack trace of the current thread.
 *//*  ww  w . ja  va 2 s .c  o  m*/
public static void printFullStackTrace() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();

    for (StackTraceElement e : backtrace) {
        System.out.printf("    Stack Trace: %s\n", e);
    }
}

From source file:Main.java

/**
 * Print diagnostic info about the current thread.
 *//* w w w  . j  a v a  2 s.  c o  m*/
public static void printThreadInfo() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();
    if (backtrace.length > 2) {
        System.out.printf("    trace[2]: %s\n", backtrace[2]);
    }
}

From source file:Main.java

/**
 * Invoke a runnable object on the AWT event thread. Does not wait
 * necessarily for it to finish. If the current thread is already the event
 * queue, the {@link Runnable} object is simply executed.
 * /*from ww  w .  java2 s. c  om*/
 * @param runnable
 *            Runnable capturing code to execute on the awt thread.
 */
public static void invokeOnEventQueue(Runnable runnable) {
    if (EventQueue.isDispatchThread()) {
        runnable.run();
    } else {
        SwingUtilities.invokeLater(runnable);
    }
}

From source file:Main.java

/**
 * Utility method used to run, synchronously, a Runnable on the main thread. Behaves exactly
 * like {@link EventQueue#invokeAndWait(Runnable)}, but can be called from the main thread as
 * well.//from ww w .  jav a2s. c om
 * 
 * @param runnable
 * @throws InterruptedException
 * @throws InvocationTargetException
 */
public static void invokeAndWait(Runnable runnable) throws InvocationTargetException, InterruptedException {
    if (EventQueue.isDispatchThread()) {
        runnable.run();
    } else {
        EventQueue.invokeAndWait(runnable);
    }
}

From source file:Main.java

/**
 * Causes <code>runnable</code> to have its <code>run</code> method called
 * in the dispatch thread of {@link Toolkit#getSystemEventQueue the system
 * EventQueue} if this method is not being called from it. This will happen
 * after all pending events are processed.
 * /*from   w w  w .j a va  2s  . c  o  m*/
 * @param runnable
 *            the <code>Runnable</code> whose <code>run</code> method should
 *            be executed synchronously on the <code>EventQueue</code>
 */
public static void invokeOnEdt(Runnable runnable) {
    boolean isEdt = EventQueue.isDispatchThread();
    if (!isEdt) {
        EventQueue.invokeLater(runnable);
    } else {
        runnable.run();
    }
}

From source file:Main.java

/**
 * Thread-friendly wrapper method for <code>Component.setEnabled</code>.
 * @param enable/*  w w w .  j ava 2 s .  c  o  m*/
 * @param components
 * @deprecated
 */
@Deprecated
public static void setEnabled(final boolean enable, final Component... components) {
    Runnable r = new Runnable() {

        public void run() {
            for (Component comp : components) {
                comp.setEnabled(enable);
            }
        }
    };

    if (EventQueue.isDispatchThread()) {
        r.run();
        return;
    }

    runTask(r, true);
}

From source file:Main.java

/**
 * Thread-friendly wrapper method for <code>JComboBox.setSelectedItem</code>.
 * @param component/*from ww w .  ja  v a  2s.c om*/
 * @param selectedItem
 *
 * @see javax.swing.JComboBox.setSelectedItem(Object)
 * @deprecated
 */
@Deprecated
public static void setSelectedItem(final JComboBox component, final Object selectedItem) {
    Runnable r = new Runnable() {

        public void run() {
            component.setSelectedItem(selectedItem);
        }
    };

    if (EventQueue.isDispatchThread()) {
        r.run();
        return;
    }

    runTask(r, true);
}

From source file:Main.java

/**
 * Runs given runnable in dispatch thread.
 *//*  www  . j  av a  2 s .  c  o m*/
public static void runInDispatchThread(final Runnable runnable) throws Exception {
    if (EventQueue.isDispatchThread()) {
        runnable.run();
    } else {
        EventQueue.invokeAndWait(runnable);
    }
}

From source file:Main.java

/**
 * Thread-friendly wrapper method for <code>AbstractButton.setSelected</code>.
 * @param selected//from w  w w . j  a va  2 s . c o m
 * @param buttons AbstractButtons to select/deselect
 *
 * @see javax.swing.AbstractButton.setSelected(boolean)
 * @deprecated
 */
@Deprecated
public static void setSelected(final boolean selected, final AbstractButton... buttons) {
    Runnable r = new Runnable() {

        public void run() {
            for (AbstractButton button : buttons) {
                button.setSelected(selected);
            }
        }
    };

    if (EventQueue.isDispatchThread()) {
        r.run();
        return;
    }

    runTask(r, true);
}