Sevi
Sevi

Reputation: 627

Javafx open another fxml in the another window with button

Is it possible in javafx to open new stages (windows) from another fxml with a button? Thanks for the answers.

Upvotes: 26

Views: 79682

Answers (2)

SimplyMe
SimplyMe

Reputation: 999

Use the code below on button click:

try {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Demo.fxml"));
    Parent root1 = (Parent) fxmlLoader.load();
    Stage stage = new Stage();
    stage.initModality(Modality.APPLICATION_MODAL);
    stage.initStyle(StageStyle.UNDECORATED);
    stage.setTitle("ABC");
    stage.setScene(new Scene(root1));  
    stage.show();
}

Upvotes: 54

Sevi
Sevi

Reputation: 627

I had to modify the code a bit and it works fine. Thank you again for the code!

public void pressButton(ActionEvent event) throws Exception {               
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/A.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setScene(new Scene(root1));  
        stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 25

Related Questions