I want to center a stage on the screen.
This is what I've tried:
public class Test extends Application
{
@Override
public void start(final Stage primaryStage)
{
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.centerOnScreen();
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
After calling centerOnScreen() the stage is too high. It does not seem to work properly. Do I need to calulate the x and y pos myself? Or how do I use this function correctly?