0

So I am using jfoenix JFXPopup component for my popup. After finally getting the popup to show. I realized that the popup is displayed relative to a owner node you pass it. My question is there any way to display the pop up relative to the scene so I can center it?

Unfortunately you can not center the popup over the owner node, you can only move it up down or left and right?

Popup relative to the search textfield in the middle of the application

//Current Code
@FXML
    protected void handleLoginButton(ActionEvent event) {
        System.out.println("Popup BUtton clicked");
        if (!loginPopup.isShowing()) {
            System.out.println("Popup is open!");
            //loginPopup.show(searchBtn, JFXPopup.PopupVPosition.BOTTOM, JFXPopup.PopupHPosition.LEFT);
            loginPopup.show(searchBtn);
        }
    }
Jon Ava
  • 13
  • 4

1 Answers1

1

Use root pane of scene when you call show, and calculate center position;

popupRoot.setPrefWidth(200);
popupRoot.setPrefHeight(300);
button.setOnAction(event -> {
    JFXPopup loginPopup = new JFXPopup(popupRoot);
    Bounds rootBounds = scene.getRoot().getLayoutBounds();
    loginPopup.show(scene.getRoot(), JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT,
            (rootBounds.getWidth() - popupRoot.getPrefWidth()) / 2,
            (rootBounds.getHeight() - popupRoot.getPrefHeight()) / 2);
});

See here to access the scene from your controller.

Community
  • 1
  • 1
monolith52
  • 886
  • 2
  • 6
  • 9