Matthew-Johnson833
Matthew-Johnson833

Reputation: 19

Show a variable's value in JavaFX

I'm currently working on a capstone project for a Java class and a problem I'm coming across frequently is displaying a variable's value in a JavaFX scene. I need a kickstart to get me moving, my google searches aren't bearing any fruit.

Thanks all :)

Upvotes: 0

Views: 1483

Answers (1)

user8701826
user8701826

Reputation:

You can use a Label. Attach it to your scene and call Label.setText(String text) with the string representation of your variable value. Here's a complete example, using a Label:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class main3 extends Application {
    static Integer variable = 250; // The value will be displayed in the window

    @Override public void start(Stage primaryStage) {
        Label variableLabel = new Label();
        variableLabel.setFont(new Font(30));
        variableLabel.setText("" + variable);
        variableLabel.setLayoutX(175);
        variableLabel.setLayoutY(125);

        Group group = new Group(variableLabel);
        Scene scene = new Scene(group, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args){
        launch();
    }
}

Result

enter image description here

Upvotes: 1

Related Questions