0

How can I center a scene within a stage?

I have the following scene:

Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(1500);
        stage.setHeight(1000);

Stage:

Parent root = FXMLLoader.load(getClass().getResource("GUI.fxml"));
Scene scene1 = new Scene(root,300,300);

Stage contains a Vbox in which all the Elements are Wrapped(look image): Image

Now the scene in itself a lot smaller than the stage.

How can I align=center the scene within the stage?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • try to wrap the vbox in another vbox/hbox that has aligment center and fits the whole scene so h(v)grow on always – fuggerjaki61 Jan 11 '20 at 13:51
  • Thats the problem i want for the scene to be ca 70% width of the stage and also be centered in the middle. Problem: for some reason the scene that was set 300,300 goes all the way till the ends of the stage which is 1500,1000. Does FXML support % as a parameter instead of pixels? –  Jan 11 '20 at 14:10
  • then try `stage.getWidth() * 0.7` or look at [this](https://stackoverflow.com/questions/9832896/how-do-specify-a-width-percentage-in-javafx-2-using-fxml/11375505) – fuggerjaki61 Jan 11 '20 at 14:12
  • **JavaFX** does not support specifying widths relative to the parent width for most layouts. A binding may be an option, but if empty space of a fixed size surrounding the control is sufficient, padding or margin may do the trick. Otherwise there's `GridPane` with `percentWidth/Height` constraints which has been mentioned in at least one question that asks specifically about this. (In your question itself there's no mention of relative size; you just ask about alignment.) – fabian Jan 12 '20 at 01:32

1 Answers1

0

EDIT (English)

The scene is always in the middle if you mean that the vbox reaches over the complete scene then you can select the vbox in scenebuilder and set HGrow on ALWAYS

ORIGINAL (German)

Die Scene ist immer in der Mitte, wenn du meinst das sich der sich die VBox über die gesamte Scene erstreckt kannst du im SceneBuilder die VBox auswählen und dann unter Layout HGrow auf ALWAYS stellen.