javafx : Managing Multiple Stages

I would like to introduce you to this WindowManager i created. its not  much but helps us to manage our windows in stacked order which is quite useful for your application because it has a clear idea of the window hierarchy. 
  1. public class WindowManager {  
  2.       
  3.     private static final WindowManager INSTANCE = new WindowManager();  
  4.       
  5.     private Stack<Stage> windows;  
  6.       
  7.     private WindowManager() {  
  8.         windows = new Stack();  
  9.     }  
  10.       
  11.     public void setMainWindow(Stage stage) {  
  12.         windows.push(stage);  
  13.     }  
  14.       
  15.     public Stage getCurrentWindow() {  
  16.         return windows.lastElement();  
  17.     }  
  18.       
  19.     public static WindowManager getInstance() {  
  20.         return INSTANCE;  
  21.     }  
  22.       
  23.     public Stage newWindow() {  
  24.         logger.info("total windows {}", windows.size());  
  25.         Stage stage = new Stage();  
  26.         stage.initModality(Modality.WINDOW_MODAL);  
  27.         stage.initOwner(windows.lastElement());  
  28.         windows.push(stage);  
  29.         stage.setOnCloseRequest(event -> {  
  30.             logger.info("removing from windows stack");  
  31.             windows.remove(stage);  
  32.         });  
  33.         return stage;  
  34.     }  
  35.   
  36.     public Stage getMainWindow() {  
  37.         return windows.firstElement();  
  38.     }  
  39.   
  40.     private void setHandlers(Scene scene) {  
  41.         scene.addEventHandler(KeyEvent.KEY_PRESSED, getEscKeyHandler());  
  42.     }  
  43.   
  44.     public EventHandler<KeyEvent> getEscKeyHandler() {  
  45.         if(escKeyHandler == null) {  
  46.             escKeyHandler = new EventHandler<KeyEvent>() {  
  47.                 @Override  
  48.                 public void handle(KeyEvent event) {  
  49.                     if(event.getCode() == KeyCode.ESCAPE) {  
  50.                         closeWindow();  
  51.                     }  
  52.                 }  
  53.             };  
  54.         }  
  55.         return escKeyHandler;  
  56.     }  
  57.       
  58.       
  59.     public Stage createWindow(Parent view) {  
  60.         Stage stage = newWindow();  
  61.         Scene scene = view.getScene();  
  62.         if(scene == null) {  
  63.             scene = new Scene(view);  
  64.             setHandlers(scene);  
  65.         }  
  66.         stage.setScene(scene);  
  67.         return stage;  
  68.     }  
  69.   
  70.     public void closeWindow() {  
  71.         windows.lastElement().close();  
  72.     }  
  73.   
  74. }  
After you create your WindowManager class, you first pass the primary stage(the one from the start method) to the WindowManager on application startup: 
  1. public class MainApp extends Application {  
  2.       
  3.     @Override  
  4.     public void start(Stage stage) throws Exception {  
  5.         WindowManager wm = WindowManager.getInstance(); // pass primary stage to WM
  6.         wm.setMainWindow(stage);  
  7.   
  8.         Parent parent = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml");  
  9.         Scene scene = new Scene(parent);
  10.         stage.setScene(scene);
  11.         stage.show();  
  12.   
  13.     }  
  14.       
  15. }  
and for each new window, you create it like this, 
  1. Parent root = FXMLLoader.load(getClass().getResource('fxml/yourscene.fxml');  
  2. Stage stage = WindowManager.getInstance().createWindow(root);  
  3. stage.show();  
each of the newly created windows can be closed by pressing the escape key. if a new window is created while already showing a window. that new window will child window of the first window and it goes on like this. creating a stack of windows. 
 
the createWindow() method can be used for creating new stages. and newWindow() method can be used for alert  boxes. 
 
the code has been highly modified for the sake of simplicity. and as i said before its not much but i hope it help someone. any doubts, ask. if you think you've got a better solution then feel free to change it.