Class TGASwingUtil


  • public class TGASwingUtil
    extends Object
    A ThreadGroupAware replacement for SwingUtilities.invokeLater(), SwingUtilities.invokeAndWait() and SwingUtilities.isEventDispatchThread() considering the correct EventQueue to use in environments where more than one EventQueue may occur, such as in Applets and WebStart.
    • Constructor Detail

      • TGASwingUtil

        public TGASwingUtil()
    • Method Detail

      • getDefaultThreadGroup

        public static ThreadGroup getDefaultThreadGroup()
        Gets the default ThreadGroup. If not explicitly set, the first one which touches invokerLater or invokeAndWait
        Returns:
        the default ThreadGroup. If not explicitly set, the first one which touches invokerLater or invokeAndWait
      • setDefaultThreadGroup

        public static void setDefaultThreadGroup​(ThreadGroup tg)
        Sets the default ThreadGroup.
        Parameters:
        tg - the default ThreadGroup.
      • callNow

        public static <V> V callNow​(ThreadGroup tg,
                                    Callable<V> c)
        Synchronously execute a Callable and return the result of the call. If this method is called off-EDT, the call will block until the callable has been called on the EDT. If it is called on-EDT, the callable will be called immediately.
        Parameters:
        tg - the ThreadGroup to be used
        c - the Callable whose run method should be executed on a ThreadGroup specific EventQueue
        Returns:
        Throwable Return the throwable the Runnable.run() method may throw as an uncaught exception (on the event dispatching thread) or null if no throwable was thrown.
      • callNow

        public static <V> V callNow​(Callable<V> c)
        Synchronously execute a Callable and return the result of the call. If this method is called off-EDT, the call will block until the callable has been called on the EDT. If it is called on-EDT, the callable will be called immediately.
        Parameters:
        c - the Callable whose Callable.call() is to be executed on a ThreadGroup specific EventQueue
        Returns:
        Throwable Return the throwable the Runnable.run() method may throw as an uncaught exception (on the event dispatching thread) or null if no throwable was thrown.
      • callLater

        public static <V> Future<V> callLater​(Callable<V> c)
        Schedule the given callable to be called on the EDT and return a Future providing access to the result of the call.
        Type Parameters:
        V - the type of result returned by the callable
        Parameters:
        c - the Callable whose Callable.call() is to be executed on a ThreadGroup specific EventQueue
        Returns:
        a Future providing access to the result of the call.
      • callLaterIfNotEDT

        public static <V> Future<V> callLaterIfNotEDT​(Callable<V> c)
        Schedule the given callable to be called on the EDT and return a Future providing access to the result of the call. If the current thread is already the event dispatch thread the Callable is called synchronously. Otherwise it will be enqueued in the event dispatch thread to be be called asynchronously.
        Type Parameters:
        V - the type of result returned by the callable
        Parameters:
        c - the Callable whose Callable.call() is to be executed on a ThreadGroup specific EventQueue
        Returns:
        a Future providing access to the result of the call.
      • callLaterIfNotEDT

        public static <V> Future<V> callLaterIfNotEDT​(ThreadGroup tg,
                                                      Callable<V> c)
        Schedule the given callable to be called on the EDT and return a Future providing access to the result of the call. If the current thread is already the event dispatch thread the Callable is called synchronously. Otherwise it will be enqueued in the event dispatch thread to be be called asynchronously.
        Type Parameters:
        V - the type of result returned by the callable
        Parameters:
        tg - the ThreadGroup to be used
        c - the Callable whose Callable.call() is to be executed on a ThreadGroup specific EventQueue
        Returns:
        a Future providing access to the result of the call.
      • callLater

        public static <V> Future<V> callLater​(ThreadGroup tg,
                                              Callable<V> c)
        Schedule the given callable to be called on the EDT and return a Future providing access to the result of the call.
        Type Parameters:
        V - the type of result returned by the callable
        Parameters:
        tg - the ThreadGroup to be used
        c - the Callable whose Callable.call() is to be executed on a ThreadGroup specific EventQueue
        Returns:
        a Future providing access to the result of the call.
      • invokeAndWait

        public static Throwable invokeAndWait​(Runnable code)
        Calls SwingUtilities.invokeAndWait(Runnable code) using the default ThreadGroup
        Parameters:
        code - the Runnable whose run method should be executed on a ThreadGroup specific EventQueue
        Returns:
        Throwable Return the throwable the Runnable.run() method may throw as an uncaught exception (on the event dispatching thread) or null if no throwable was thrown.
      • invokeAndWait

        public static Throwable invokeAndWait​(ThreadGroup threadGroup,
                                              Runnable code)
        Calls SwingUtilities.invokeAndWait(Runnable code) considering the ThreadGroup to be used
        Parameters:
        threadGroup - the ThreadGroup to be used
        code - the Runnable whose run method should be executed on a ThreadGroup specific EventQueue
        Returns:
        Throwable Return the throwable the Runnable.run() method may throw as an uncaught exception (on the event dispatching thread) or null if no throwable was thrown.
      • invokeLater

        public static void invokeLater​(Runnable code)
        Calls SwingUtilities.invokeLater(Runnable code) using the default ThreadGroup.
        Parameters:
        code - the Runnable whose run method should be executed on a ThreadGroup specific EventQueue
      • invokeLater

        public static void invokeLater​(ThreadGroup threadGroup,
                                       Runnable code)
        Calls SwingUtilities.invokeLater(Runnable code) considering the ThreadGroup to be used
        Parameters:
        threadGroup - the ThreadGroup to be used
        code - the Runnable whose run method should be executed on a ThreadGroup specific EventQueue
      • destroy

        public static void destroy()
        Stops the ExecutionQueues redirecting Runnables to EventQueues and performs cleanup.
      • destroy

        public static void destroy​(ThreadGroup tg)
        Stops the ExecutionQueues redirecting Runnables to EventQueues and performs cleanup for the ThreadGroup supplied.
        Parameters:
        tg - the threadGroup to be used for removing the execution queues
      • isActive

        public static boolean isActive()
        Gets the active flag.
        Returns:
        the active flag.
      • setActive

        public static void setActive​(boolean active)
        Sets the active flag. if not active, invokeLater, invokeAndWait and isEventDispatchThread will be called on the regular SwingUtilities.
        Parameters:
        active - the active flag to set.
      • isEventDispatchThread

        public static boolean isEventDispatchThread()
        Checks whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup. Returns false, if not.
        Returns:
        whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup. Returns false, if not.
      • isEventDispatchThread

        public static boolean isEventDispatchThread​(ThreadGroup tg)
        Checks whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup. Returns false, if not.
        Parameters:
        tg - the threadGroup to be checked
        Returns:
        whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup. Returns false, if not.
      • isDestroyed

        public static boolean isDestroyed()
      • init

        public static void init()
        Sets the destroyed flag to false so Runnables are directed at the EventQueues.
        See Also:
        destroy()