JavaFx: set window title in fxml file

I am just starting to use JavaFx for a new application.

I know how to set the window title in java code, but how to set it in the fxml file?

Thank you for your help.

Edit: Here is the code I have

@Override public void start(Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); primaryStage.setTitle(applicationName); primaryStage.setScene(new Scene(root)); primaryStage.show(); } 

I just want to set the header in Main.fxml.

+6
source share
1 answer

To set the scene title in FXML, you need to create a stage in FXML, for example:

 <?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.VBox?> <?import javafx.stage.Stage?> <?import javafx.scene.Scene?> <?import javafx.scene.control.Label?> <Stage title="Some Stage"> <scene> <Scene> <VBox xmlns:fx="http://javafx.com/fxml"> <children> <Label text="John Doe"/> </children> </VBox> </Scene> </scene> </Stage> 

If you only create the root element of the scene (in my example, VBox) via FXML, and then put it in the scene later, as you do (which is the usual way), then it is impossible to set the title in FXML directly (without the code behind).

+9
source

Source: https://habr.com/ru/post/952000/


All Articles