Search code examples
javajavafxjfoenix

JavaFX - how to center JFXpopup on stage and dynamic size of popup


I want to make a JFXpopup at the center of the screen with a dynamic width & height. This is what I currently have:

Application first look This is the application's first look. To open the popup I have to click an item in the contextmenu:

context menu and after that you get to see the popup: popup

The popup is centered on the x axis, but apparently not on the y axis. Also when I resize my stage and reopen the popup the size doesn't change. popup2

Now for some code:

contextMenu.getNewLeaf().setOnAction(event -> {
            popup.setPopupContent(createRegistrationFormPane());

            popup.setAutoFix(true);

            double width = globals.getPrimaryStage().getWidth() / 1.5;
            double height = globals.getPrimaryStage().getHeight() / 1.5;

            System.out.println("stage - width: " + globals.getPrimaryStage().getWidth() + "height: " + globals.getPrimaryStage().getHeight());
            System.out.println("width: " + width + " height: " + height);

            popup.getPopupContent().setPrefWidth(width);
            popup.getPopupContent().setPrefHeight(height);

            double anchorX = (globals.getPrimaryStage().getWidth() - width) / 2;
            double anchorY = (globals.getPrimaryStage().getHeight() - height) / 2;

            popup.show(rootPane, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT, anchorX, anchorY);

            item.getChildren().add(new TreeItem<>(new Twitter()));
            contextMenu.freeActionListeners();
        });

globals.getPrimaryStage() returns the stage.
rootPane is the well ... the first Pane.

Here are some values from the system.out.println() :

stage - width: 800.0height: 550.4000244140625
width: 533.3333333333334 height: 366.933349609375

stage - width: 1339.199951171875height: 684.7999877929688  
width: 892.7999674479166 height: 456.5333251953125

As you can see these values do change, but the size of the popup doesn't change.

Any help would be appreciated.


Solution

  • First of all I notices that getting the stage height includes the topbar ( - [] X ). That is why the popup doesn't vertically center properly. To fix that I added .getScene() and then .getHeight().

    Secondly I now added the changed it to the following: I made it so that the popup is created when the button is clicked. So instead of making 1 jfxpopup I just override the pointer to the popup and let the garbage collector do the rest.

    // Option add new leaf
    CONTEXT_MENU.getNewLeaf().setOnAction(event -> {
      popup = new JFXPopup();
      try {
        popup.setPopupContent((Region) UTIL
            .getPopupView(UTIL.getView("view/application/popup/FormTest.fxml"), popup));
      } catch (IOException e) {
        e.printStackTrace();
      }
      popup.setAutoFix(true);
      // Width & height of the popup
      double width = GLOBALS.getPrimaryStage().getScene().getWidth() / 1.5;
      double height = GLOBALS.getPrimaryStage().getScene().getHeight() / 1.5;
      popup.getPopupContent().setPrefWidth(width);
      popup.getPopupContent().setPrefHeight(height);
      // X & y axis of the popup
      double anchorX = (GLOBALS.getPrimaryStage().getScene().getWidth() - width) / 2;
      double anchorY = (GLOBALS.getPrimaryStage().getScene().getHeight() - height) / 2;
      popup.show(ROOT_PANE, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT, anchorX,
          anchorY);
      item.getChildren().add(new TreeItem<>(new Twitter()));
      CONTEXT_MENU.freeActionListeners();
    });
    

    1 quistion : When I you say popup.hide() does that remove the instance of that popup? Right now I just let the garbage collector do its thing. Just curious.