Class SwingBackgroundTask<V>

  • Type Parameters:
    V - Type of result generated by the task.

    public abstract class SwingBackgroundTask<V>
    extends Object
    A task that is executed on a background thread and then updates a Swing GUI. A task may only be executed once.
    • Constructor Detail

      • SwingBackgroundTask

        protected SwingBackgroundTask()
    • Method Detail

      • waitForCompletion

        public void waitForCompletion()
                               throws InterruptedException
        Waits for the execution of this task to complete. If the execute() method has not yet been invoked, this method will block indefinitely.
        Throws:
        InterruptedException - If the thread executing the task is interrupted.
      • performTask

        protected abstract V performTask()
                                  throws Exception
        Performs the processing of the task and returns a result. Implement in sub-classes to provide the task logic. This method will run on a background thread and not on the Event Dispatch Thread and therefore should not manipulate any Swing components.
        Returns:
        The result of executing this task.
        Throws:
        Exception - The task may throw an exception, in which case the onError(Throwable) method will be invoked instead of postProcessing(Object).
      • postProcessing

        protected void postProcessing​(V result)
        This method is invoked, on the Event Dispatch Thread, after the task has been executed. This empty default implementation should be over-ridden in sub-classes in order to provide GUI updates that should occur following successful task completion.
        Parameters:
        result - The result from the performTask() method.
      • onError

        protected void onError​(Throwable throwable)
        This method is invoked, on the Event Dispatch Thread, if there is an exception or error executing the performTask() method. This default implementation displays a message dialog with details of the exception. It may be over-ridden in sub-classes.
        Parameters:
        throwable - The exception or error that was thrown while executing the task.