0

So I was wondering if it is possible to get the window title (like shown on the image) into a string from the controller. The image

Avi
  • 2,611
  • 1
  • 14
  • 26
  • Note that for the approach presented in the linked question you can also use an injected node that is part of the scene... – fabian Oct 16 '19 at 17:30

2 Answers2

1

The title of an JavaFx-Application is set in the class javafx.stage.Stage via stage.setTitle("hello world");.

That means that you can get the title of your stage via stage.getTitle();.

To get your stage, you could do something like this: Assuming that you have your starting class named Main and a controller with a method doSomething.

In your class Main, you can save your complete stage.

public class Main extends Application {

    private static Stage stage;

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 400, 400);
        stage = primaryStage;
        stage.setTitle("hello World");
        stage.setScene(scene);
        stage.show();
    }

    public static Stage getStage() {
        return stage;
    }

    // some other methods
}

That means that your starting stage (primaryStage) is saved in the variable stage.

In your controller, you can easily get your stage (and the title) by calling the getter.

public void doSomething() {
    String title = Main.getStage().getTitle();
    //  some other code
}
alea
  • 980
  • 1
  • 13
  • 18
0

getTitle() method will give a window title String.

George Weekson
  • 483
  • 3
  • 13