I don't know how to close the main window in javafxml

MatiiMann :

I don't know how to close the main window in java fxml. This part of code is in the class Main.

public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    Parent root2 = FXMLLoader.load(getClass().getResource("2ndwin.fxml"));
    Scene scene  = new Scene(root);
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(scene);
    primaryStage.show();

    Scene scene2 = new Scene(root2);
    secondaryStage.setScene(scene2);
}

public void show(){
    secondaryStage.show();
}

I've got this. In controller i did this:Main m = new Main(); m.show();

but i don't know still how I can close primaryStage.

Please help me or tell how can i creat new window and close old window. I think this what i want to do isn't correct but i came up with it myself.

JSGoodbody :

I do it by using an object in the stage you want to close to get the window

    Window currentStage = OBJECTINSCENE.getScene().getWindow();

(Replace 'OBJECTINSCENE' with the id of anything in your scene). This gives you a reference to the stage you have open. Then call

currentStage.hide();

To close the stage when you want to.

So your show function would be as follows

public void show(){
    Window currentStage = OBJECTINSCENE.getScene().getWindow();
    secondaryStage.show();
    currentStage.hide();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344618&siteId=1