Search code examples
buttonjavafxmenuitemstage

javafx retain root while adding a secondary stage


I have a growing application with a menu bar with Home, Chapters, Calculator, Glossary, Help. Everything is working fine including the Chapters with 14 items.

Also the Calculator that is a source package. It opens but I lose the stage. I would like it to open and able to be dragged around the screen.

This is the code I am using:

else if(e.getSource()==mbarcalculator){
        stage = (Stage) root.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("/javafxcalc/FXMLcalc.fxml"));

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
        }

I have read a lot of posts but haven't found a simple answer.


Solution

  • After a lot of patience and perseverance I have a solution that works and one I can understand.

    else if(e.getSource()==mbarcalculator){
    Stage secondaryStage = new Stage(); Parent root2 = FXMLLoader.load(getClass().getResource("/javafxcalc/FXMLcalc.fxml")); Scene scene2 = new Scene(root2); secondaryStage.setScene(scene2); secondaryStage.show();
    }

    I took a small section of code from Jose Pereda's answer Display two windows at the same time "on fullscreen" with JavaFx Scene Builder 2.0

    I have tested it and it works nicely.