Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX window setTitle

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?

like image 512
Mr.Fireman Avatar asked Mar 15 '15 00:03

Mr.Fireman


People also ask

How do you set the title of the stage primary stage?

We use Stage primStage = (Stage) input. getScene(). getWindow(); within the doStuff() method to get a handle on the primary stage. That solution is the simplest, the current accepted solution might be overkill in a lot of cases.

What is primary stage JavaFX?

The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application. Stage objects must be constructed and modified on the JavaFX Application Thread.

What is stage and scene in JavaFX?

Stage , represents a window in a JavaFX desktop application. Inside a JavaFX Stage you can insert a JavaFX Scene which represents the content displayed inside a window - inside a Stage .

What is StackPane JavaFX?

StackPane class is a part of JavaFX. StackPane class lays out its children in form of a stack. The new node is placed on the top of the previous node in a StackPane. StackPane class inherits Pane Class.


2 Answers

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);
    }
}
like image 73
ItachiUchiha Avatar answered Sep 21 '22 11:09

ItachiUchiha


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.

like image 21
davidrmcharles Avatar answered Sep 20 '22 11:09

davidrmcharles