Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make constant variables in Javafx for XML files

StackPane layoutY="70.0" prefHeight="479.0". I want to make the values (70.0) and (479.0) static in a Java file so I can use them for other files.

Is this possible?

like image 875
Kyle Wolff Avatar asked Jul 07 '15 03:07

Kyle Wolff


People also ask

How do you make a variable constant in Java?

To make any variable a constant, we must use 'static' and 'final' modifiers in the following manner: Syntax to assign a constant value in java: static final datatype identifier_name = constant; The static modifier causes the variable to be available without an instance of it's defining class being loaded.

Does JavaFX use XML?

JavaFX FXML is an XML format that enables you to compose JavaFX GUIs in a fashion similar to how you compose web GUIs in HTML. FXML thus enables you to separate your JavaFX layout code from the rest of your application code.

Which option is valid constant declaration in Java?

In Java, to declare any variable as constant, we use static and final modifiers. It is also known as non-access modifiers. According to the Java naming convention the identifier name must be in capital letters.

What keyword makes a variable a constant?

Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.


1 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 as follows:

<StackPane>
    <prefHeight>
        <SomeClass fx:constant="DEFAULT_HEIGHT" />
    </prefHeight>
</StackPane>

Make sure you have the appropriate import in the fxml file for the class you are using.

like image 200
James_D Avatar answered Oct 01 '22 08:10

James_D