Java Reference
In-Depth Information
The delay property controls how much time must elapse after the start() call on the scheduled service before
the task starts to run. The period property controls how much time must elapse after one run of the task before the
next run of the task can start. The period measures the differences between the start of one run and the start of the
next run. The delay and period are object properties of type Duration . If no failure condition occurs while the task is
executing, the ScheduledService will keep on repeating the task indefinitely.
If a failure condition occurs while the task is executing, what happens next is controlled by the restartOnFailure
property. If this property is false , the ScheduledService will remain in the FAILED state and nothing more will happen.
If the restartOnFailure property is true , the ScheduledService will rerun the task again. The rerunning of failed tasks
is controlled by the backOffStrategy , maximumFailureCount , and maximumCumulativePeriod properties. The back
off strategy is simply a lambda expression that takes the ScheduledService as an argument and returns a Duration ,
which is set as the value of the cumulativePeriod property. The rerunning of the task will start when the elapsed
time since the start of the last failed run of the task reaches cumulativePeriod . In addition to cumulativePeriod ,
the ScheduledService also keeps track of the currentFailureCount property, which is the number of consecutive
failed runs in the current sequence of failed runs. If the rerunning of the task is successful, ScheduledService will
go back to its normal behavior of running the task in period time intervals. Otherwise—that is, if the rerun failed
again— ScheduledService will ask the backOffStrategy for a new cumulativePeriod , and rerun again. If the
currentFailureCount reaches maximumFailureCount , or if the cumulativePeriod becomes greater than or equal to the
maximumCumulativePeriod , the ScheduledService will go into the FAILED state and nothing more will happen.
Three back off strategies are provided. They are constants in ScheduledService . The LINEAR_BACKOFF_STRATEGY
returns ever longer Durations that grow linearly. The EXPONENTIAL_BACKOFF_STRATEGY returns ever longer
Durations that grow exponentially. The LOGARITHMIC_BACKOFF_STRATEGY returns ever longer Durations that grow
logarithmically. You can easily define your own back off strategies.
You can reset and restart a ScheduledService by calling reset() and start() methods.
Mixing JavaFX with Other GUI Toolkits
Having examined the threading paradigm of the JavaFX runtime and ways to execute code from the JavaFX
application thread, we now look at how to make JavaFX coexist with some other GUI toolkits. JavaFX provides classes
and frameworks that make it possible to mix JavaFX with Swing or SWT. You can embed a JavaFX scene in a Swing
application. You can embed a JavaFX scene in a SWT application. And you can embed Swing components in a
JavaFX application.
Embedding JavaFX Scenes in Swing Applications
JavaFX supports embedding a JavaFX scene into a Swing application through the javafx.embed.swing package of
classes. This is a pretty small package that includes one public class for embedding JavaFX scenes into
Swing— JFXPanel —and another class— SwingNode —for embedding Swing components into JavaFX applications. The
JFXPanel class extends javax.swing.JComponent , and as such can be placed in a Swing program just as any other
Swing component. JFXPanel can also host a JavaFX scene, and as such can add a JavaFX scene to a Swing program.
However, this Swing program with a JavaFX scene embedded in it needs both the Swing runtime to make its
Swing portion function correctly, and the JavaFX runtime to make the JavaFX portion function correctly. Therefore it
has both the Swing Event Dispatching Thread (EDT) and the JavaFX Application Thread. The JFXPanel class does a
two-way translation of all the user events between Swing and JavaFX.
Just as JavaFX has the rule that requires all access to live scenes to be done in the JavaFX Application Thread,
Swing has the rule that requires all access to Swing GUIs to be done in the EDT. You still need to jump the thread if
you want to alter a Swing component from a JavaFX event handler or vice versa. The proper way to execute a piece of
code on the JavaFX Application Thread, as we saw earlier, is to use Platform.runLater() . The proper way to execute a
piece of code on the Swing EDT is to use EventQueue.invokeLater() .
In this section, we convert a pure Swing program into a Swing and JavaFX hybrid program. We start off with the
Swing program in Listing 7-11, which is very similar to the ResponsiveUIExample program.
 
Search WWH ::




Custom Search