Java Reference
In-Depth Information
Figure 11-3. The simplest JavaFX application using a Nashorn script without writing even
one line of code
Importing JavaFX Types
You can use the fully qualified name of the JavaFX classes or import them using the
Java.type() function. In the previous section, you used the fully qualified names of all
JavaFX classes. The following snippet of code shows the two approaches to create a Label
in JavaFX:
// Using the fully qualified name of the Label class
var msg = new javafx.scene.control.Label("Hello JavaFX!");
// Using Java.type() function
var Label = Java.type("javafx.scene.control.Label");
var msg = new Label("Hello JavaFX!");
It may be cumbersome to type the fully qualified names of all JavaFX classes. Aren't
scripts supposed to be shorter than Java code? Nashorn has a way to make your JavaFX
script shorter. It includes several script files that import the JavaFX types as their simple
names. You will need to load those script files using the load() method before using
the simple names of JavaFX classes in your script. For example, Nashorn includes a
fx:controls.js script file that imports all JavaFX control classes as their simple class
names. Table 11-1 contains the list of script files and the classes/packages that they
import.
 
Search WWH ::




Custom Search