Search code examples
javalayoutjavafxjavafx-2gridpane

Javafx gridpane layout allignment


I have a small question. I am trying to create a good GUI (which you can resize) with JavaFX 2.0 but I have a small problem. I am using a gridpane but when i put multiple buttons in it and when I resize the screen to a smaller size the buttons will slide into each other at the left size of the screen. Can I set a minimum size for a cell or something, I am using an FXML for the design. Thank you very much!


Solution

  • You can set the minimum width of a column by specifying a set of ColumnConstraints objects on the GridPane.

    For example, something like:

    <GridPane hgap"5" vgap="5">
    <Label text="First Name:" GridPane.rowIndex="0" GridPane.columnIndex="0" />
    <TextField GridPane.rowIndex="0" GridPane.columnIndex="1" />
    
    <Label text="Last Name:" GridPane.rowIndex="1" GridPane.columnIndex="0" />
    <TextField GridPane.rowIndex="1" GridPane.columnIndex="1" />
    
    <columnConstraints>
    <ColumnConstraints minWidth="100" halignment="RIGHT" />
    <ColumnConstraints minWidth="150" halignment="LEFT" />
    </columnConstraints>
    
    </GridPane>
    

    You can similarly control the layout of the rows, if desired.

    See the Javadocs for GridPane and ColumnConstraints to see other properties that can be controlled this way.