1

I am making an application which uses a settings menu, which i have implemented using a different stage from the main stage of the application. It is shown on a button click, and i was wondering if javafx offers a way to make the stage appear in the center of the screen by default, for me it appears in the top left. It seems like I'm missing something, because this sort of thing happens with alert dialogues for other applications, but the Dialog (or Alert) class is not ideal, as my goal doesn't really fit into a single input, like what might be seen in a dialog box.

Relevant Code:

Button settings = new Button();
Image image = new Image(getClass().getResourceAsStream("gear.png"));
settings.setGraphic(new ImageView(image));
settings.setFocusTraversable(false);
grid.add(settings, 1, 2);                   
settings.setOnAction(e -> {                             
        Stage settingsStage = new Stage();
        settingsStage.initModality(Modality.APPLICATION_MODAL);
        //Neither centerOnScreen() or the setX, setY lines change where the stage appears
        //setY and setX work themselves out if they do not rely on stage
        //settingsStage.centerOnScreen();
        //settingsStage.setX(stage.getX() + stage.getWidth() / 2 - settingsStage.getWidth() / 2);
        //settingsStage.setY(stage.getY() + stage.getHeight() / 2 - settingsStage.getHeight() / 2);
        settingsStage.show();
    });

For reference, i am using javafx 8 on a mac

hjobrien
  • 141
  • 1
  • 10

1 Answers1

0

This question should have what you are looking for: Center location of stage

You just need to set the stage's coordinates in its start method.

Community
  • 1
  • 1
Blue
  • 646
  • 5
  • 10
  • Thanks, that was it, but to follow up, i hadn't previously defined the size of my stage, it was dependent on a scene that it contained, which made the other question's answer not work. (it used stage.getHeight() and other things to put the stage in the exact middle), but if i replaced that logic with a number it worked fine, so if i don't explicitly define a size of my stage, do getHeight and getWidth return 0? if so, why? that seems counterintuitive – hjobrien Aug 10 '15 at 05:19