I am trying to define dynamic evaluated variables with fx:define, but I can't get a variable to be evaluated from another one, I don't know if it's even possible?
<GridPane hgap="${10*m.dp}" vgap="${10*m.dp}" xmlns="http://javafx.com/javafx/8.0.51" xmlns:fx="http://javafx.com/fxml/1">
<fx:define>
<Measurement fx:id="m" />
<!-- This works, but is not what I need -->
<Double fx:id="width" fx:value="300" />
<!-- This doesn't work -->
<!-- <Double fx:id="width" fx:value="${300*m.dp}" /> -->
</fx:define>
<padding>
<Insets bottom="$width" left="$width" right="$width" top="$width" />
</padding>
<Text text="hello" />
<Button GridPane.rowIndex="1" text="button" prefWidth="${300*m.dp}" />
<Button GridPane.rowIndex="2" text="button2" prefWidth="$width" />
</GridPane>
What I want here is to compute the width from my computed dp (Density independent pixel - value is 1 in HD screen, 2 in 4K screen, 0.xx on my 1600px width screen). The "width" variable I want is to be used in very many component in my real case, this is why I wanted a variable - for concision.
The java code to run it
public class Measurement {
private double dp;
public Measurement(){
Screen primary = Screen.getPrimary();
dp=primary.getBounds().getWidth()/1920;
}
/**
* Equivalent of 1 px in 1920.
*/
public double getDp(){
return dp;
}
public void setDp (double dp) {
this.dp = dp;
}
}
public class MApplication extends Application {
public static void main (String[] args) {
launch (args);
}
@Override
public void start (Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
Parent root = fxmlLoader.load(getClass().getResource("index.fxml").openStream());
Scene scene = new Scene(root, 300, 275);
primaryStage.setMaximized(true);
primaryStage.setScene(scene);
primaryStage.show ();
}
}
Actually, it s not clear to me which and where expression can be used, I see many answers here :Is it possible to use arithmetic expression in FXML?
here:Bind Font Size in JavaFX?
I don't understand in which case I can use the expression language,is there a specification?
Edited: I adedd a Link to javafx-8 documentation, And I removed my obsolete comment on javaFX-2