How can I make persistent variables in Javafx for XML files
2 answers
If your constant is defined in a class:
public class SomeClass {
public static final double DEFAULT_HEIGHT = 479 ;
// ...
}
then you can access it in FXML like this:
<StackPane>
<prefHeight>
<SomeClass fx:constant="DEFAULT_HEIGHT" />
</prefHeight>
</StackPane>
Make sure you have the appropriate imports in your fxml file for the class you are using.
+6
source to share
James_D showed you a way to do this with a custom class. Another way to do it in fxml is to define your own variables. But they are not shared between files.
Instead of this
<StackPane layoutY="70.0" prefHeight="479.0">
Do you want to have
<StackPane layoutY="$variable" prefHeight="$variable">
You can do it like this:
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" id="AnchorPane" prefHeight="200" prefWidth="320" fx:controller="javafxapplication22.FXMLDocumentController">
<fx:define>
<Double fx:id="layoutY" fx:value="70.0"/>
<Double fx:id="prefHeight" fx:value="479.0"/>
</fx:define>
<children>
<StackPane layoutY="$layoutY" prefHeight="$prefHeight"/>
<Pane layoutY="$layoutY" prefHeight="$prefHeight"/>
</children>
</AnchorPane>
+6
source to share