Class PlatformHelper

java.lang.Object
com.dua3.utility.fx.PlatformHelper

public final class PlatformHelper extends Object
The PlatformHelper class provides utility methods for performing tasks on the JavaFX application thread. It provides methods for running tasks synchronously or asynchronously on the JavaFX application thread, as well as checking if the current thread is the FX Application Thread.
  • Method Details

    • runAndWait

      public static void runAndWait(Runnable action)
      Run a task on the JavaFX application thread and wait for completion.

      Note:

      • This method will block the calling thread until the task completes.
      • Any runtime exceptions thrown by the task will be rethrown by this method.
      • Consider using runLater(Runnable) to avoid executing tasks out of order.
      Parameters:
      action - the task to run
      Throws:
      NullPointerException - if action is null
      RuntimeException - if the task throws one
    • runAndWait

      public static <T extends @Nullable Object> T runAndWait(Supplier<T> action)
      Run a task on the JavaFX application thread and return the result.

      Note:

      • This method will block the calling thread until the task completes.
      • Any runtime exceptions thrown by the task will be rethrown by this method.
      • Consider using runLater(Runnable) to avoid executing tasks out of order.
      Type Parameters:
      T - the result type
      Parameters:
      action - the task to run
      Returns:
      the result returned by action
      Throws:
      NullPointerException - if action is null
      RuntimeException - if the task throws one
    • runLater

      public static void runLater(Runnable action)
      Run a task on the JavaFX application thread and log any exceptions.
      Parameters:
      action - the task to run
      Throws:
      NullPointerException - if action is null
    • runLater

      public static void runLater(Runnable action, Consumer<Throwable> errorHandler)
      Run a task on the JavaFX application thread and handle any exceptions.

      Note that the error handler is called on the JavaFX application thread son there is no use in simply mapping the exception to a different type. Also it is not possible to pass the exception to the caller as the task is run asynchronously.

      If the exception should be thrown to the caller, runAndWait(Runnable) has to be used instead.

      Parameters:
      action - the task to run
      errorHandler - the error handler to use
      Throws:
      NullPointerException - if action is null
    • checkApplicationThread

      public static void checkApplicationThread()
      Checks if the current thread is the FX Application Thread. Throws an exception if it is not.
    • shutdown

      public static boolean shutdown(long timeout, TimeUnit unit)
      Shut down the JavaFX platform.
      Parameters:
      timeout - the maximum time to wait for the platform to shut down
      unit - the time unit of the timeout argument
      Returns:
      true if the platform shut down within the timeout, false otherwise