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?
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.
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.
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 .
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.
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With