1

Am doing a project in JavaFX. Each window of my project has variable sizes. While running the project, the first window appears at the center of the screen. This is the screen shot.

enter image description here

But when I move to next page its size is different and so it appears like -

enter image description here

As you can see this window appeared to the right side of the screen. But I need to display every new window appear at the center of the screen. How can I do it ?

TomJ
  • 1,803
  • 14
  • 37
  • 57
  • 3
    use `stage.centerOnScreen();` or http://stackoverflow.com/questions/13702191/center-location-of-stage – Anshul Parashar Apr 21 '14 at 09:45
  • @AnshulParashar I open each new window using this code - if (usr1.equals(username) && pwd.equals(password)) { ((Stage) btn.getScene().getWindow()).setScene(new Scene( new TutorPage())); So how will I use 'stage.centerOnScreen();' here ? – TomJ Apr 21 '14 at 09:55
  • `(Stage) btn.getScene().getWindow().centerOnScreen();` – Anshul Parashar Apr 21 '14 at 09:58
  • @AnshulParashar Should I use this instead of ' (Stage) btn.getScene().getWindow()).setScene(new Scene( new TutorPage())); ' ? – TomJ Apr 21 '14 at 10:02

1 Answers1

1

You can do this if you use this code;

yourScene.getWindow().centerOnScreen();

or

((Stage)(sceneObject.getScene.getWindow())).centerOnScreen();

sceneObject is any object which is in your scene.

Bent
  • 81
  • 8