JavaFX open new window
Asked Answered
J

3

51

Looking at this code they show a way to display a new window after a login. When username and password are correct it opens new dialog. I want a button click to open new dialog, without checking for username and password.

Jerryjerrybuild answered 23/2, 2013 at 14:48 Comment(1)
The link is brokenTannen
B
105

If you just want a button to open up a new window, then something like this works:

btnOpenNewWindow.setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent event) {
        Parent root;
        try {
            root = FXMLLoader.load(getClass().getClassLoader().getResource("path/to/other/view.fxml"), resources);
            Stage stage = new Stage();
            stage.setTitle("My New Stage Title");
            stage.setScene(new Scene(root, 450, 450));
            stage.show();
            // Hide this current window (if this is what you want)
            ((Node)(event.getSource())).getScene().getWindow().hide();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Bolte answered 23/2, 2013 at 15:1 Comment(7)
Is there a way to 'drop' the existing window instead of hiding it?Buzzer
@Buzzer closeing and hideing a window is the same thing. ­this answer is good at explaining this concept.Bolte
I mean drop it as-in delete the instance. I have a long running app (weeks) and I don't need any resources un-garbage collected to gather lint.Buzzer
That's exactly it. hideing it or closeing it will remove it. Just delete any reference to this window/stage (such you would do with any other java object).Bolte
I'm trying to hide window from menuitem ActionEvent but getting exception "menuitem cannot be casted to Node". Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.NodeTerpene
Is there any reason for not declaring root just where you initialize it?Mccomb
@Mccomb In this example not really. But assuming you want to do something else with root outside of the try block then it would be useful.Bolte
M
21

I use the following method in my JavaFX applications.

newWindowButton.setOnMouseClicked((event) -> {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("NewWindow.fxml"));
        /* 
         * if "fx:controller" is not set in fxml
         * fxmlLoader.setController(NewWindowController);
         */
        Scene scene = new Scene(fxmlLoader.load(), 600, 400);
        Stage stage = new Stage();
        stage.setTitle("New Window");
        stage.setScene(scene);
        stage.show();
    } catch (IOException e) {
        Logger logger = Logger.getLogger(getClass().getName());
        logger.log(Level.SEVERE, "Failed to create new Window.", e);
    }
});
Mavis answered 15/3, 2017 at 19:7 Comment(0)
P
4

The code below worked for me I used part of the code above inside the button class.

public Button signupB;

public void handleButtonClick (){

    try {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("sceneNotAvailable.fxml"));
        /*
         * if "fx:controller" is not set in fxml
         * fxmlLoader.setController(NewWindowController);
         */
        Scene scene = new Scene(fxmlLoader.load(), 630, 400);
        Stage stage = new Stage();
        stage.setTitle("New Window");
        stage.setScene(scene);
        stage.show();
    } catch (IOException e) {
        Logger logger = Logger.getLogger(getClass().getName());
        logger.log(Level.SEVERE, "Failed to create new Window.", e);
    }

}

}
Phantasmagoria answered 11/10, 2020 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.