Java Reference
In-Depth Information
SwingUtilities.invokeLater , and a lambda expression encapsulates the
Runnable that is used to set the Swing content.
It is possible to interact with JavaFX content from within Swing code as well. To
do so, you must run the JavaFX code within the JavaFX application thread by making a
call to the javafx.application.Platform class and invoking the run-
Later() method, passing a Runnable . For instance, in the example code, the but-
ton in the Swing form can call back to the JavaFX label to change the text using the
following code. Note that the JavaFX label is a public field, so it is accessible directly
from within the Swing class.
JButton button = new JButton("Submit");
button.addActionListener((event) -> {
Platform.runLater(()-> {
UserEntryForm.fxLabel.setText("Message from
Swing form...");
});
});
Note By default, the JavaFX application thread and the Swing Event Dispatch
Thread (EDT) are separated. The EDT does not run the GUI code for a Swing applica-
tion. However, in JavaFX, the platform GUI thread runs the application code. There is
an experimental setting that enables single threading mode, which allows the JavaFX
platform GUI thread to become the EDT when using Swing and JavaFX together. To
enable the experimental setting, execute your code with the following op-
tion: Djavafx.embed.singleThread=true
By utilzing the new features of JavaFX 8, you can generate a JavaFX application
that contains embedded Swing code that can communicate directly with the JavaFX
code.
Summary
JavaFX is the successor to the Java Swing API. It enables developers to produce soph-
isticated and powerful user interfaces for the next generation of applications. This
chapter provided you with a basic understanding of JavaFX, along with some of the
Search WWH ::




Custom Search