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

/**
 * 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. The call blocks until this has
 * happened. This method will throw an Error if called from the event
 * dispatcher thread.//from   w ww.j  a va  2 s. 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 invokeOnEdtAndWait(Runnable runnable) {
    boolean isEdt = EventQueue.isDispatchThread();
    if (!isEdt) {
        try {
            EventQueue.invokeAndWait(runnable);
        } catch (InterruptedException e) {
            System.err.println("EDT task interrupted");
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            System.err.println("EDT task invocation exception");
            e.printStackTrace();
        }
    } else {
        runnable.run();
    }
}

From source file:Main.java

public Point getClick() {
    EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
    System.out.println(eq.isDispatchThread());
    while (true) {
        try {//from  w w  w. j a va 2s  . c om
            AWTEvent evt = eq.getNextEvent();
            if (evt.getID() == MouseEvent.MOUSE_PRESSED) {
                MouseEvent mevt = (MouseEvent) evt;
                Point p = mevt.getPoint();
                Point top = getRootPane().getLocation();
                p.x -= top.x;
                p.y -= top.y;
                return p;
            }
        } catch (InterruptedException e) {
        }
    }
}

From source file:Main.java

/**
 * Causes runnable to have its run method called in the dispatch thread of
 * the event queue. This will happen after all pending events are processed.
 * The call blocks until this has happened.
 *///from  w  w  w.  j a  v  a  2s.com
public static void invokeAndWait(final Runnable runnable) {
    if (EventQueue.isDispatchThread()) {
        runnable.run();
    } else {
        try {
            EventQueue.invokeAndWait(runnable);
        } catch (InterruptedException exception) {
            // Someone don't want to let us sleep. Go back to work.
        } catch (InvocationTargetException target) {
            final Throwable exception = target.getTargetException();
            if (exception instanceof RuntimeException) {
                throw (RuntimeException) exception;
            }
            if (exception instanceof Error) {
                throw (Error) exception;
            }
            // Should not happen, since {@link Runnable#run} do not allow checked exception.
            throw new UndeclaredThrowableException(exception, exception.getLocalizedMessage());
        }
    }
}

From source file:Main.java

/**
 * Indicate if the current thread is the EDT.
 *
 * @return {@code true} if the current thread is the EDT else {@code false}.
 *///from  www. j  a  v a 2s. com
public static boolean isEDT() {
    return EventQueue.isDispatchThread();
}

From source file:EventDispatcher.java

public static void fireEvent(final Dispatcher dispatcher, final List listeners, final Object evt,
        final boolean useEventQueue) {
    if (useEventQueue && !EventQueue.isDispatchThread()) {
        Runnable r = new Runnable() {
            public void run() {
                fireEvent(dispatcher, listeners, evt, useEventQueue);
            }//w  ww .j  a  va  2 s .c  om
        };
        try {
            EventQueue.invokeAndWait(r);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // Assume they will get delivered????
            // be nice to wait on List but how???
        } catch (ThreadDeath td) {
            throw td;
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return;
    }

    Object[] ll = null;
    Throwable err = null;
    int retryCount = 10;
    while (--retryCount != 0) {
        // If the thread has been interrupted this can 'mess up'
        // the class loader and cause this otherwise safe code to
        // throw errors.
        try {
            synchronized (listeners) {
                if (listeners.size() == 0)
                    return;
                ll = listeners.toArray();
                break;
            }
        } catch (Throwable t) {
            err = t;
        }
    }
    if (ll == null) {
        if (err != null)
            err.printStackTrace();
        return;
    }
    dispatchEvent(dispatcher, ll, evt);
}

From source file:Main.java

/**
 * Run the runnable in EventDispatch Thread.
 * If the current thread is EventDispatch, it will run
 * immediately otherwise it will be queued in the DispatchThread
 * The difference with VFSManager.runInAWTThread() method is that
 * this one will not wait for IO Request before being executed
 *
 * @param runnable the runnable to run - it should return something meaningful from 
 *    toString() so that we can display it in the Task Monitor.
 *///  w w  w.  j av  a2 s . com
public static void runInDispatchThread(Runnable runnable) {
    if (EventQueue.isDispatchThread())
        runnable.run();
    else
        EventQueue.invokeLater(runnable);
}

From source file:Main.java

/**
 * Moves the supplied <code>JSplitPane</code> divider to the specified <code>proportion</code>.
 * Valid values for <code>proportion</code> range from <code>0.0F<code>
 * to <code>1.0F</code>.  For example, a <code>proportion</code> of <code>0.3F</code> will move the
 * divider to 30% of the "size" (<i>width</i> for horizontal split, <i>height</i> for vertical split) of the
 * split container that contains the specified <code>Dockable</code>.  If a <code>proportion</code> of less
 * than <code>0.0F</code> is supplied, the value </code>0.0F</code> is used.  If a <code>proportion</code>
 * greater than <code>1.0F</code> is supplied, the value </code>1.0F</code> is used.
 * <br/>/*from w  w  w . ja  va 2  s .  c om*/
 * This method should be effective regardless of whether the split layout in question has been fully realized
 * and is currently visible on the screen.  This should alleviate common problems associated with setting
 * percentages of unrealized <code>Component</code> dimensions, which are initially <code>0x0</code> before
 * the <code>Component</code> has been rendered to the screen.
 * <br/>
 * If the specified <code>JSplitPane</code> is <code>null</code>, then this method returns with no action
 * taken.
 *
 * @param split the <code>JSplitPane</code> whose divider location is to be set.
 * @param proportion a double-precision floating point value that specifies a percentage,
 * from zero (top/left) to 1.0 (bottom/right)
 * @see #getSplitPaneSize(JSplitPane)
 * @see JSplitPane#setDividerLocation(double)
 */
public static void setSplitDivider(final JSplitPane split, float proportion) {
    if (split == null)
        return;

    proportion = Math.max(0f, proportion);
    final float percent = Math.min(1f, proportion);
    int size = getSplitPaneSize(split);

    if (split.isVisible() && size > 0 && EventQueue.isDispatchThread()) {
        split.setDividerLocation(proportion);
        split.validate();
        return;
    }

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            setSplitDivider(split, percent);
        }
    });
}

From source file:Main.java

/** 
 * Runs the task synchronously if the current thread is the event thread; otherwise passes it to the
 * event thread to be run asynchronously after all events already on the queue have been processed.
 *//*from w  w  w .ja va 2s. c  o  m*/
public static void invokeLater(Runnable task) {
    if (EventQueue.isDispatchThread()) {
        task.run();
    } else {
        EventQueue.invokeLater(task);
    }
}

From source file:Main.java

static public boolean isSwingThread() {
    return EventQueue.isDispatchThread();
}

From source file:net.landora.video.utils.UIUtils.java

public static void invokeLaterInSwingThread(Runnable r) {
    if (EventQueue.isDispatchThread()) {
        r.run();/*  w  w w  .j a v  a  2s .c om*/
    } else {
        SwingUtilities.invokeLater(r);
    }
}