Java Reference
In-Depth Information
We have replaced the long sleep with code that executes in a worker thread. After sleeping for three seconds, the
worker thread calls the runLater() method of the javafx.application.Platform class, passing it another Runnable
that toggles the rounded corners of the rectangle. Because the long-running computation is done in a worker thread,
the event handler is not blocking the JavaFX application thread. The change of fill is now reflected immediately in
the UI. Because the Platform.runLater() call causes the Runnable to be executed on the JavaFX application thread,
the change to the rounded corners is reflected in the UI after three seconds. The reason we have to execute the
Runnable on the JavaFX application thread is that it modifies the state of a live scene.
The Platform class includes the following other helpful utility methods:
public static boolean isFxApplicationThread() returns true if it is executed on the
JavaFX application thread and false otherwise.
public static boolean isSupported(ConditionalFeature) tests whether the execution
environment supports a ConditionalFeature . Testable ConditionalFeatures include
GRAPHICS , CONTROLS , MEDIA , WEB , SWT , SWING , FXML , SCENE3D , EFFECT , SHAPE_CLIP , INPUT_METHOD ,
TRANSPARENT_WINDOW , UNIFIED_WINDOW , TWO_LEVEL_FOCUS , VIRTUAL_KEYBOARD , INPUT_TOUCH ,
INPUT_MULTITOUCH , and INPUT_POINTER .
public static void exit() , if called after the application's start() method has been called,
causes the application's stop() method to be executed on the JavaFX application thread
before the JavaFX application thread and other JavaFX platform threads are taken down. If the
application's start() method has not been called yet, the application's stop() method may
not be called.
public static boolean isImplicitExit() and public static void
setImplicitExit(boolean) tests and sets the implicit exit flag. When this flag is true, the
JavaFX runtime will shut down when the last application window is closed. Otherwise you
have to explicitly call Platform.exit() to shut down the JavaFX runtime. The default value of
this flag is true .
if you are familiar with swing programming, you should see the similarity between JavaFX's
Platform.runLater() and swing's EventQueue.invokerLater() , or SwingUtilities.invokeLater() .
Note
Now that we have solved our problem with Runnable and Thread and Platform.runLater() , it is time to see how
we can use JavaFX's built-in worker threading framework to solve the problem in a more flexible and elegant way.
Understanding the javafx.concurrent Framework
The JavaFX worker threading framework in the javafx.concurrent package combines the versatility and flexibility
of the Java concurrency framework introduced in Java 5 with the convenience of the JavaFX properties and bindings
framework to produce an easy-to-use toolset that is aware of the JavaFX application threading rules and also
very easy to use. It consists of one interface, Worker , and three abstract base classes, Task<V> , Service<V> , and
ScheduledService<V> , that implement the interface.
 
 
Search WWH ::




Custom Search