GridPane lays out its children within a flexible grid of rows and columns.
A child may be placed anywhere within the GridPane and may span multiple rows/columns (default span is 1) and its placement within the grid is defined by it's layout constraints:
| Constraint | Description |
|---|---|
| columnIndex | column where child's layout area starts. |
| rowIndex | row where child's layout area starts. |
| columnSpan | the number of columns the child's layout area spans horizontally. |
| rowSpan | the number of rows the child's layout area spans vertically. |
The total number of rows/columns does not need to be specified up front as the gridpane will automatically expand/contract the grid to accommodate the content.
In order to add new Nodes to a GridPane the layout constraints on the children should be set using the static method of GridPane class, then those children can be added to a GridPane instance.
GridPane gridPane = new GridPane();
// Set the constraints: first row and first column
Label label = new Label("Example");
GridPane.setRowIndex(label, 0);
GridPane.setColumnIndex(label, 0);
// Add the child to the grid
gridpane.getChildren().add(label);
GridPane provides convenient methods to combine these steps:
gridPane.add(new Button("Press me!"), 1, 0); // column=1 row=0
The GridPane class also provides static setter methods to set the row- and columnspan of child elements:
Label labelLong = new Label("Its a long text that should span several rows");
GridPane.setColumnSpan(labelLong, 2);
gridPane.add(labelLong, 0, 1); // column=0 row=1
By default, rows and columns will be sized to fit their content. In case of the need of the explicit control of row and column sizes, RowConstraints and ColumnConstraints instances can be added to the GridPane. Adding these two constraints will resize the example above to have the first column 100 pixels, the second column 200 pixels long.
gridPane.getColumnConstraints().add(new ColumnConstraints(100));
gridPane.getColumnConstraints().add(new ColumnConstraints(200));
By default the GridPane will resize rows/columns to their preferred sizes even if the gridpane is resized larger than its preferred size. To support dynamic column/row sizes, both contstaints class provides three property: min size, max size and preferred size.
Additionally ColumnConstraints provides setHGrow and RowConstraints provides setVGrow methods to affect the priority of the growing and shrinking. The three pre-defined priorities are:
ColumnConstraints column1 = new ColumnConstraints(100, 100, 300);
column1.setHgrow(Priority.ALWAYS);
The column defined above have a minimal size of 100 pixels and it will always try to grow until it reaches its maximal 300 pixel width.
It is also possible to define percentage sizing for rows and columns. The following example defines a GridPane where the first column fills 40% of the gridpane's width, the second one fills the 60%.
GridPane gridpane = new GridPane();
ColumnConstraints column1 = new ColumnConstraints();
column1.setPercentWidth(40);
ColumnConstraints column2 = new ColumnConstraints();
column2.setPercentWidth(60);
gridpane.getColumnConstraints().addAll(column1, column2);
The alignment of Nodes can be defined by using the setHalignment (horizontal) method of ColumnConstraints class and setValignment (vertical) method of RowConstraints class.
ColumnConstraints column1 = new ColumnConstraints();
column1.setHalignment(HPos.RIGHT);
RowConstraints row1 = new RowConstraints();
row1.setValignment(VPos.CENTER);