Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx : Set window title in fxml file

Tags:

javafx-2

I'm just starting to use JavaFx for a new application.

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

Thanks 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 set the title in Main.fxml.

like image 559
Ekans Avatar asked Aug 19 '13 08:08

Ekans


1 Answers

to set the title of the stage in FXML, you need to construct the stage in FXML, like this:

<?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 construct the root element of the scene (in my example, the VBox) via FXML and then put it into a scene afterwards like you do it (which is the common way), then it is impossible to set the title in FXML directly (without code behind).

like image 186
zhujik Avatar answered Sep 25 '22 13:09

zhujik