JavaScript script in FXML

I am trying to run the following example from an FXML link :

This example consists of declaring a String variable in a JavaScript script and then using it in FXML with the $ operator to display a string in a shortcut.

The problem is that when I run this example with Java 8u40, the shortcut is empty and not showing the declared string.

JavaFxComponent.java:

 public class JavaFxComponent extends VBox { public JavaFxComponent() throws Exception { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/JavaFxComponent.fxml")); fxmlLoader.setRoot(this); fxmlLoader.load(); } } 

JavaFxComponent.fxml:

 <?xml version="1.0" encoding="UTF-8"?> <?language javascript?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <fx:root type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml"> <fx:script> var myText = "This is the text of my label."; </fx:script> <Label text="$myText" /> </fx:root> 

JavaFxTest:

 public class JavaFxTest extends Application { @Override public void start(Stage primaryStage) throws Exception { primaryStage.setScene(new Scene(new JavaFxComponent())); primaryStage.show(); } public static final void main(String[] args) { launch(args); } } 
+7
javafx javafx-8
source share
1 answer

It seems that " var myText " does not create a link in the area where " $ " searches. This is probably not the answer you were looking for, but I find it useful to mention alternatives to those who stumble upon the same problem, at least until it is resolved, or someone sheds more light on this question.


  •  <fx:define> <String fx:id="myText" fx:value="This is the text of my label." /> </fx:define> <Label text="$myText" /> 

  •  <Label fx:id="myLabel" /> <fx:script> myLabel.text = "This is the text of my label."; </fx:script> 

Note: for the first working method, <?import java.lang.String?> be imported.

+3
source share

All Articles