2 Replies Latest reply: Jan 15, 2013 10:43 PM by likejiujitsu RSS

    how to close a popup dialog?

    likejiujitsu
      Looking at the Scenebuilder1.1 Login sample, a new scene is created which is placed on stage.
      How would such a popup get closed and get back the original main screen?
          private Initializable replaceSceneContent(String fxml) throws Exception {
              FXMLLoader loader = new FXMLLoader();
              InputStream in = Main.class.getResourceAsStream(fxml);
              loader.setBuilderFactory(new JavaFXBuilderFactory());
              loader.setLocation(Main.class.getResource(fxml));
              AnchorPane page;
              try {
                  page = (AnchorPane) loader.load(in);
              } finally {
                  in.close();
              }
              // store the stage height in case the user has resized the window
              double stageWidth = stage.getWidth();
              if (!Double.isNaN(stageWidth)) {
                  stageWidth -= (stage.getWidth() - stage.getScene().getWidth());
              }
              double stageHeight = stage.getHeight();
              if (!Double.isNaN(stageHeight)) {
                  stageHeight -= (stage.getHeight() - stage.getScene().getHeight());
              }
              Scene scene = new Scene(page);
              if (!Double.isNaN(stageWidth)) {
                  page.setPrefWidth(stageWidth);
              }
              if (!Double.isNaN(stageHeight)) {
                  page.setPrefHeight(stageHeight);
              }
              stage.setScene(scene);
              stage.sizeToScene();
              return (Initializable) loader.getController();
          }
        • 1. Re: how to close a popup dialog?
          jsmith
          How would such a popup get closed and get back the original main screen?
          That is not code for a popup.
          That is code to replace the contents of the original "main screen" with new content loaded from fxml (sizing the newly loaded content to fit inside the existing stage).

          In general, if you had a dialog popped up, you can call dialog.hide() to hide your dialog window (which is pretty much equivalent to closing the dialog).
          http://docs.oracle.com/javafx/2/api/javafx/stage/Window.html#hide%28%29
          Or if the dialog has a close button in it's frame, the usef can click on that to close it.
          • 2. Re: how to close a popup dialog?
            likejiujitsu
            Thanks for your reply. I rewrote the sample to do this - created a new Stage, and it worked. :)
                <T extends ControllerBase> T replaceSceneContent(T controller, String fxml, String message) {
                    try {
                        AnchorPane page;
                        FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
                        page = (AnchorPane) loader.load();
                        Stage dialog = new Stage(StageStyle.UTILITY);
                        dialog.initOwner(MainPane.primaryStage);
                        dialog.getIcons().add(new Image("/resources/my-icon.gif"));
                        dialog.initModality(Modality.WINDOW_MODAL);
                        Scene scene = new Scene(page);
            
                        dialog.setScene(scene);
                        T c = loader.getController();
                        c.stage = dialog;
                        c.setMessage(message);
                        dialog.showAndWait();
                        return c;
                    } catch (IOException ex) {
                        Logger.getLogger(main_screenController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return null;
                }