JavaFX window setTitle

20,166

Solution 1

Exposing properties using static, just for the sake of exposing, may be considered as a bad design. You have different ways to achieve the same, for example, expose a method from the Window class which sets the stage title.

public class Window extends Application {

    private Stage stage;

    @Override
    public void start(Stage foablak) throws Exception {
        stage = foablak;
        Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));
        Scene scene = new Scene(root);

        foablak.setScene(scene);
        foablak.setWidth(900);
        foablak.setHeight(700);
        foablak.setResizable(false);
        foablak.setTitle("Window");

        foablak.show();
    }

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

    public void setStageTitle(String newTitle) {
        stage.setTitle(newTitle);
    }
}

Solution 2

Yes, you can. Inside of your Application.start() method, save a reference to your primary Stage that you can access elsewhere, and then call Stage.setTitle().

class MyApplication extends Application {

    public static Stage primaryStage;

    @Override
    public void start(Stage primaryStage) {
        MyApplication.primaryStage = primaryStage;

        // ...
    }

}

MyApplication.primaryStage.setTitle("New Title");

As an aside, I would avoid calling your class Window, as that is the name of one of the JavaFX classes.

Solution 3

The following may not be the solution you were looking for, but it might be useful for some of the developers:

  • Scenario: There is only 1 JavaFX app; The app needs to be run multiple times; You want to differentiate who is running the app
  • Attention: Pay attention of the running order of the Main and
    Controller class
  • Solution: i. In the Controller Class, declare a private static variable, e.g., private static String strWho; ii. Expose strWho by providing a getter method. e.g.: public static String getWho(){ return strWho; }; iii. Implement the initialize method for the Controller, and based on your need, assign a distinct value each time you run the JavaFX app. eg., @FXML public void initialize() { strWho = "you need to have logic here, to have a distinct value each time you run the app"; }
  • In the Main start method, right before you call the stage.show, set the title. eg: primaryStage.setTitle(Controller.getWho())); primaryStage.show();
  • One way to implement the logic for distinct value of the strWho each time you run the app: You can have a TextInputDialog in the Controller's initialize method, to accept user input, by asking for a name etc.
Share:
20,166
Mr.Fireman
Author by

Mr.Fireman

Updated on January 31, 2020

Comments

  • Mr.Fireman
    Mr.Fireman over 4 years

    I have a main class which is the following:

    public class Window extends Application {
    
    
        @Override
        public void start(Stage foablak) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("Foablak.fxml"));
            Scene scene = new Scene(root);
    
            foablak.setScene(scene);
            foablak.setWidth(900);
            foablak.setHeight(700);
            foablak.setResizable(false);
            foablak.setTitle("Window");
    
            foablak.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    How can I update the Title of the from another .java class without closing the window and open a new one?