JavaFx : Set window title in fxml file

15,750

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).

Share:
15,750

Related videos on Youtube

Ekans
Author by

Ekans

Updated on June 04, 2022

Comments

  • Ekans
    Ekans about 2 years

    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.

  • Ekans
    Ekans almost 11 years
    Thanks. Sorry for the trouble.
  • zhujik
    zhujik almost 11 years
    No need to apologize, I'm glad I could help.
  • jewelsea
    jewelsea almost 10 years
    See JavaFX Using alternative FXML structure for Title (Stage is root) for an example of how to use Sebastian's FXML.
  • m0skit0
    m0skit0 over 8 years
    java.lang.ClassCastException: javafx.stage.Stage cannot be cast to javafx.scene.layout.Pane