Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: keep all components in center even when resizing a window

I want all my components (TextFields, labels, etc.) stay in the center with fixed size. I am making it in scene builder and it works when I see the preview, but when I launch it, components don't stay in the center when I resize it.

Please see the picture here: http://postimg.org/image/r42kvvfbf/

FXML file:

<AnchorPane id="AnchorPane" fx:id="root" maxHeight="-1.0" maxWidth="-1.0" minHeight="400.0" minWidth="600.0" prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="myController">
  <children>
    <GridPane alignment="CENTER" prefHeight="-1.0" prefWidth="-1.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <children>
        <VBox alignment="CENTER_LEFT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="-1.0" prefWidth="-1.0" spacing="10.0" GridPane.columnIndex="0" GridPane.rowIndex="0">
          <children>
            <Label text="ID number" />
            <TextField fx:id="id" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="210.0" promptText="" />
            <Label text="Password" />
            <TextField fx:id="password" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="210.0" promptText="" />
            <HBox id="HBox" alignment="CENTER" spacing="5.0">
              <children>
                <Button mnemonicParsing="false" onAction="#loginAction" text="Вход" textAlignment="CENTER" textOverrun="ELLIPSIS" />
              </children>
            </HBox>
          </children>
          <padding>
            <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
          </padding>
        </VBox>
      </children>
      <columnConstraints>
        <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
  </children>
</AnchorPane>

I have tried using StackPane and only with VBox as well, but I can't achieve what I want. Please help!

like image 497
Cortez Nix Avatar asked May 10 '14 16:05

Cortez Nix


1 Answers

It is SOLVED. Maybe this might be helpful for others.

It appears that I am using Group as a root for my Scene. As said in JavaFX documentation: "If a Group is used as the root, the contents of the scene graph will be clipped by the scene's width and height and changes to the scene's size (if user resizes the stage) will not alter the layout of the scene graph".

Link: http://docs.oracle.com/javafx/2/api/javafx/scene/Scene.html

like image 161
Cortez Nix Avatar answered Sep 19 '22 03:09

Cortez Nix