1

I am trying to center a Popup in the center of a Stage, but I can't do it right because I need the size of the Popup and it is coming as size 0 (popup.getWidth()=0 and popup.getHeight()=0).

How to get the correct size?

My code is below:

                Popup popup = new Popup();
                popup.setAutoFix(true);
                popup.setAutoHide(true);
                popup.setHideOnEscape(true);
                Label label = new Label("Empty indentation char!");
                label.setOpacity(100);
                label.setStyle("-fx-background-color: cornsilk;");
                popup.getContent().add(label);
                Point2D center = Utils.getCenter(mainClass.getOptionsStage());
                popup.show(mainClass.getOptionsStage(),
                        center.getX() - popup.getWidth() / 2,
                        center.getY() - popup.getHeight() / 2);
  • Utils.getCenter() returns the center point of a window.
ceklock
  • 6,143
  • 10
  • 56
  • 78
  • 1
    Exact duplicate? If you do like the link is saying the Popup component will not be centered. Show me how you can center a Popup like that, I challenge you! – ceklock Dec 16 '12 at 23:29

1 Answers1

3

Popup doesn't know his bounds until he is being shown.

Try to move it after being shown:

popup.show(mainClass.getOptionsStage());
popup.setX(center.getX() - popup.getWidth() / 2);
popup.setY(center.getY() - popup.getHeight() / 2);
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • 1
    Thanks, it works. I don't like to set the location of components after they are visible, but I think this is really the only way for now. – ceklock Dec 12 '12 at 18:41