Class AsyncTaskStage<T>

java.lang.Object
javafx.stage.Window
javafx.stage.Stage
cl.obcom.desktopfx.jfx.AsyncTaskStage<T>
Type Parameters:
T - the type of the result of the asynchronous task.
All Implemented Interfaces:
EventTarget

public final class AsyncTaskStage<T> extends Stage
Modal Stage used to execute an asynchronous task.

The following code fragment demonstrates typical usage:

 // Define callable to be executed asynchronously
 Callable<Integer> callable = () -> computeResult();

 // Create AsyncTaskStage to execute callable with owner and cancel button
 AsyncTaskStage<Integer> asyncStage = new AsyncTaskStage<>(owner, true, callable);

 // Display AsyncTaskStage, execute and obtain result
 Integer result = asyncStage.showAndWaitForTaskValue();
  • Constructor Details

    • AsyncTaskStage

      public AsyncTaskStage(Object owner, boolean withCancel, Callable<T> callable)
      Creates the new AsyncTaskStage with supplied callable.

      Argument owner can be a Node, a Scene, a Window or null. If owner is a Node then "node.getScene().getWindow()" will become the owner. If owner is a Scene then "scene.getWindow()" will become the owner. Finally, if owner is null then this AsyncTaskStage will become a top-level, unowned stage. See method Stage.initOwner(Window) for more detalis.

      Parameters:
      owner - the owner of this stage (can be null).
      withCancel - if true display a cancel button.
      callable - the callable to be executed asynchronously.
      Throws:
      NullPointerException - if callable is null.
    • AsyncTaskStage

      public AsyncTaskStage(Object owner, boolean withCancel, Task<T> task)
      Creates the new AsyncTaskStage with supplied task.

      Argument owner can be a Node, a Scene, a Window or null. If owner is a Node then "node.getScene().getWindow()" will become the owner. If owner is a Scene then "scene.getWindow()" will become the owner. Finally, if owner is null then this AsyncTaskStage will become a top-level, unowned stage. See method Stage.initOwner(Window) for more detalis.

      Parameters:
      owner - the owner of this stage (can be null).
      withCancel - if true display a cancel button.
      task - the task to be executed asynchronously.
      Throws:
      NullPointerException - if task is null.
  • Method Details

    • showAndWaitForTaskValue

      public T showAndWaitForTaskValue() throws Exception
      Displays this modal stage and executes the asynchronous task.
      Returns:
      the task result or null if the task was cancelled.
      Throws:
      Exception - if the asynchronous task completed with error.