A child may be placed anywhere within the grid and may span multiple rows/columns. Children may freely overlap within rows/columns and their stacking order will be defined by the order of the gridpane's children list (0th node in back, last node in front).
GridPane may be styled with backgrounds and borders using CSS. See
Region superclass for details.
A child's placement within the grid is defined by it's layout constraints:
| Constraint | Type | Description |
|---|---|---|
| columnIndex | integer | column where child's layout area starts. |
| rowIndex | integer | row where child's layout area starts. |
| columnSpan | integer | the number of columns the child's layout area spans horizontally. |
| rowSpan | integer | the number of rows the child's layout area spans vertically. |
If the row/column indices are not explicitly set, then the child will be placed in the first row/column. If row/column spans are not set, they will default to 1. A child's placement constraints can be changed dynamically and the gridpane will update accordingly.
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.
To use the GridPane, an application needs to set the layout constraints on the children and add those children to the gridpane instance. Constraints are set on the children using static setter methods on the GridPane class:
GridPane gridpane = new GridPane();
// Set one constraint at a time...
// Places the button at the first row and second column
Button button = new Button();
GridPane.setRowIndex(button, 0);
GridPane.setColumnIndex(button, 1);
// or convenience methods set more than one constraint at once...
Label label = new Label();
GridPane.setConstraints(label, 2, 0); // column=2 row=0
// don't forget to add children to gridpane
gridpane.getChildren().addAll(button, label);
Applications may also use convenience methods which combine the steps of
setting the constraints and adding the children:
GridPane gridpane = new GridPane();
gridpane.add(new Button(), 1, 0); // column=1 row=0
gridpane.add(new Label(), 2, 0); // column=2 row=0
GridPane gridpane = new GridPane();
gridpane.getColumnConstraints().add(new ColumnConstraints(100)); // column 0 is 100 wide
gridpane.getColumnConstraints().add(new ColumnConstraints(200)); // column 1 is 200 wide
By default the gridpane will resize rows/columns to their preferred sizes (either
computed from content or fixed), even if the gridpane is resized larger than
its preferred size. If an application needs a particular row or column to
grow if there is extra space, it may set its grow priority on the RowConstraints
or ColumnConstraints object. For example:
GridPane gridpane = new GridPane();
ColumnConstraints column1 = new ColumnConstraints(100,100,Double.MAX_VALUE);
column1.setHgrow(Priority.ALWAYS);
ColumnConstraints column2 = new ColumnConstraints(100);
gridpane.getColumnConstraints().addAll(column1, column2); // first column gets any extra width
Note
Nodes spanning multiple rows/columns will be also size to the preferred sizes. The affected rows/columns are resized by the following priority: grow priorities, last row. This is with respect to row/column constraints.
GridPane gridpane = new GridPane();
ColumnConstraints column1 = new ColumnConstraints();
column1.setPercentWidth(50);
ColumnConstraints column2 = new ColumnConstraints();
column2.setPercentWidth(50);
gridpane.getColumnConstraints().addAll(column1, column2); // each get 50% of width
If a percentage value is set on a row/column, then that value takes precedent and the
row/column's min, pref, max, and grow constraints will be ignored.
Note that if the sum of the widthPercent (or heightPercent) values total greater than 100, the values will be treated as weights. e.g. if 3 columns are each given a widthPercent of 50, then each will be allocated 1/3 of the gridpane's available width (50/(50+50+50)).
A gridpane's parent will resize the gridpane within the gridpane's resizable range during layout. By default the gridpane computes this range based on its content and row/column constraints as outlined in the table below.
| width | height | |
|---|---|---|
| minimum | left/right insets plus the sum of each column's min width. | top/bottom insets plus the sum of each row's min height. |
| preferred | left/right insets plus the sum of each column's pref width. | top/bottom insets plus the sum of each row's pref height. |
| maximum | Double.MAX_VALUE | Double.MAX_VALUE |
A gridpane's unbounded maximum width and height are an indication to the parent that it may be resized beyond its preferred size to fill whatever space is assigned to it.
GridPane provides properties for setting the size range directly. These properties default to the sentinel value USE_COMPUTED_SIZE, however the application may set them to other values as needed:
gridpane.setPrefSize(300, 300);
// never size the gridpane larger than its preferred size:
gridpane.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
Applications may restore the computed values by setting these properties back
to USE_COMPUTED_SIZE.
GridPane does not clip its content by default, so it is possible that children's bounds may extend outside its own bounds if a child's min size prevents it from being fit within it space.
An application may set additional constraints on children to customize how the child is sized and positioned within the layout area established by it's row/column indices/spans:
| Constraint | Type | Description |
|---|---|---|
| halignment | javafx.geometry.HPos | The horizontal alignment of the child within its layout area. |
| valignment | javafx.geometry.VPos | The vertical alignment of the child within its layout area. |
| hgrow | javafx.scene.layout.Priority | The horizontal grow priority of the child. |
| vgrow | javafx.scene.layout.Priority | The vertical grow priority of the child. |
| margin | javafx.geometry.Insets | Margin space around the outside of the child. |
By default the alignment of a child within its layout area is defined by the alignment set for the row and column. If an individual alignment constraint is set on a child, that alignment will override the row/column alignment only for that child. Alignment of other children in the same row or column will not be affected.
Grow priorities, on the other hand, can only be applied to entire rows or columns. Therefore, if a grow priority constraint is set on a single child, it will be used to compute the default grow priority of the encompassing row/column. If a grow priority is set directly on a RowConstraint or ColumnConstraint object, it will override the value computed from content.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private ObjectProperty | |
| private Orientation | |
| private static final String | |
| private static final String | |
| private final ObservableList | columnConstraints
ColumnConstraints instances can be added to explicitly control individual column sizing and layout behavior. |
| private Priority[] | |
| private GridPane. | |
| private GridPane. | |
| private double | |
| private double[] | |
| private GridPane. | |
| private GridPane. | |
| private GridPane. | |
| private static final String | |
| private static final String | |
| private static final Color | |
| private static final double | |
| private Group | |
| private BooleanProperty | |
| private static final String | |
| private DoubleProperty | |
| private static final String | |
| private static final String | |
| private static final Callback | |
| private boolean | |
| private int | |
| private int | |
| private boolean | |
| public static final int | REMAINING
Sentinel value which may be set on a child's row/column span constraint to indicate that it should span the remaining rows/columns. |
| private static final String | |
| private static final String | |
| private List | |
| private final ObservableList | rowConstraints
RowConstraints instances can be added to explicitly control individual row sizing and layout behavior. |
| private Priority[] | |
| private double[] | |
| private GridPane. | |
| private double[] | |
| private GridPane. | |
| private double[] | |
| private double | |
| private double[] | |
| private GridPane. | |
| private static final String | |
| private DoubleProperty | |
| private static final String |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | add(Node
the node being added to the gridpane child, int the column index position for the child within the gridpane, counting from 0 columnIndex, int the row index position for the child within the gridpane, counting from 0 rowIndex)Adds a child to the gridpane at the specified [column, row] position. |
| public void | add(Node
the node being added to the gridpane child, int the column index position for the child within the gridpane, counting from 0 columnIndex, int the row index position for the child within the gridpane, counting from 0 rowIndex, int the number of columns the child's layout area should span colspan, int the number of rows the child's layout area should span rowspan)Adds a child to the gridpane at the specified [column, row] position and spans. |
| public void | |
| public void | |
| private double | |
| private double | |
| public final ObjectProperty | Returns: the alignment of the grid within the gridpane's width and heightThe alignment of the grid within the gridpane's width and height. |
| public static void | |
| private void | |
| private GridPane. | |
| private GridPane. | |
| private GridPane. | |
| protected double | computeMinHeight(double
the width that should be used if min height depends
on it width)Overrides javafx. Computes the minimum height of this region. |
| private GridPane. | |
| protected double | computeMinWidth(double
the height that should be used if min width depends
on it height)Overrides javafx. Computes the minimum width of this region. |
| private GridPane. | |
| protected double | computePrefHeight(double
the width that should be used if preferred height depends
on it width)Overrides javafx. Computes the preferred height of this region for the given width; Region subclasses should override this method to return an appropriate value based on their content and layout strategy. |
| private GridPane. | |
| protected double | computePrefWidth(double
the height that should be used if preferred width depends
on it height)Overrides javafx. Computes the preferred width of this region for the given height. |
| private GridPane. | |
| private GridPane. | |
| pack-priv static void | |
| private GridPane. | |
| private GridPane. | |
| private Line | |
| private static Priority[] | |
| pack-priv static void | |
| public final Pos | |
| private Pos | |
| private double | |
| public final Bounds | Returns: the bounds of the cell at columnIndex and rowIndex.the column index position for the cell within this
GridPane, counting from 0 columnIndex, int the row index position for the cell within this GridPane,
counting from 0 rowIndex)Returns the bounds of the cell at the specified column and row position. |
| public static List | Returns: theCssMetaDataHides javafx. Gets the |
| public final ObservableList | |
| public final int | |
| private HPos | |
| public static Integer | Returns: the column index for the child or null if no column index was setthe child node of a gridpane child)Returns the child's column index constraint if set. |
| private double | |
| private double | |
| private double | |
| public static Integer | Returns: the column span for the child or null if no column span was setthe child node of a gridpane child)Returns the child's column-span constraint if set. |
| public Orientation | Returns: null unless one of its children has a content bias.Overrides javafx. Returns the orientation of a node's resizing bias for layout purposes. |
| public List | getCssMetaData()
Overrides javafx. Implements javafx. This method should delegate to |
| pack-priv double[][] | |
| public static HPos | Returns: the horizontal alignment for the child or null if no alignment was setthe child node of a gridpane child)Returns the child's halignment constraint if set. |
| public final double | |
| public static Priority | |
| public static Insets | |
| pack-priv static int | |
| private int | |
| pack-priv static int | |
| private static int | |
| private static Priority | |
| pack-priv static int | |
| private int | |
| pack-priv static int | |
| private static int | |
| private static Priority | |
| private int | |
| private int | |
| public final ObservableList | |
| public final int | |
| public static Integer | Returns: the row index for the child or null if no row index was setthe child node of a gridpane child)Returns the child's row index constraint if set. |
| private double | |
| private double | |
| private double | |
| public static Integer | Returns: the row span for the child or null if no row span was setthe child node of a gridpane child)Returns the child's row-span constraint if set. |
| private VPos | |
| private double | |
| private double | |
| public static VPos | Returns: the vertical alignment for the child or null if no alignment was setthe child node of a gridpane child)Returns the child's valignment constraint if set. |
| public final double | |
| public static Priority | |
| public final BooleanProperty | Returns: true if lines are displayed to show the gridpane's rows and columnsFor debug purposes only: controls whether lines are displayed to show the gridpane's rows and columns. |
| private double | |
| private double | |
| private double | |
| private double | |
| public final DoubleProperty | Returns: the width of the horizontal gaps between columnsThe width of the horizontal gaps between columns. |
| public static Boolean | Returns: the vertical fill policy for the child or null if no policy was setthe child node of a gridpane child)Returns the child's vertical fill policy if set |
| public static Boolean | Returns: the horizontal fill policy for the child or null if no policy was setthe child node of a gridpane child)Returns the child's horizontal fill policy if set |
| public final boolean | |
| private boolean | |
| protected void | layoutChildren()
Overrides javafx. Invoked during the layout pass to layout the children in this
|
| private void | layoutGridLines(GridPane.
|
| public void | requestLayout()
Overrides javafx. Requests a layout pass to be performed before the next scene is rendered. |
| public final void | |
| public static void | setColumnIndex(Node
the child node of a gridpane child, Integer the column index of the child value)Sets the column index for the child when contained by a gridpane so that it will be positioned starting in that column of the gridpane. |
| public static void | setColumnSpan(Node
the child node of a gridpane child, Integer the column span of the child value)Sets the column span for the child when contained by a gridpane so that it will span that number of columns horizontally. |
| public static void | setConstraints(Node
the child node of a gridpane child, int the column index position for the child columnIndex, int the row index position for the child rowIndex)Sets the column,row indices for the child when contained in a gridpane. |
| public static void | setConstraints(Node
the child node of a gridpane child, int the column index position for the child columnIndex, int the row index position for the child rowIndex, int the number of columns the child should span columnspan, int the number of rows the child should span rowspan)Sets the column, row, column-span, and row-span value for the child when contained in a gridpane. |
| public static void | setConstraints(Node
the child node of a gridpane child, int the column index position for the child columnIndex, int the row index position for the child rowIndex, int the number of columns the child should span columnspan, int the number of rows the child should span rowspan, HPos the horizontal alignment of the child halignment, VPos the vertical alignment of the child valignment)Sets the grid position, spans, and alignment for the child when contained in a gridpane. |
| public static void | setConstraints(Node
the child node of a gridpane child, int the column index position for the child columnIndex, int the row index position for the child rowIndex, int the number of columns the child should span columnspan, int the number of rows the child should span rowspan, HPos the horizontal alignment of the child halignment, VPos the vertical alignment of the child valignment, Priority the horizontal grow priority of the child hgrow, Priority the vertical grow priority of the child vgrow)Sets the grid position, spans, and alignment for the child when contained in a gridpane. |
| public static void | setConstraints(Node
the child node of a gridpane child, int the column index position for the child columnIndex, int the row index position for the child rowIndex, int the number of columns the child should span columnspan, int the number of rows the child should span rowspan, HPos the horizontal alignment of the child halignment, VPos the vertical alignment of the child valignment, Priority the horizontal grow priority of the child hgrow, Priority the vertical grow priority of the child vgrow, Insets the margin of space around the child margin)Sets the grid position, spans, alignment, grow priorities, and margin for the child when contained in a gridpane. |
| public static void | setFillHeight(Node
the child node of a gridpane child, Boolean the vertical fill policy or null for unset value)Sets the vertical fill policy for the child when contained by a gridpane. |
| public static void | setFillWidth(Node
the child node of a gridpane child, Boolean the horizontal fill policy or null for unset value)Sets the horizontal fill policy for the child when contained by a gridpane. |
| public final void | |
| public static void | setHalignment(Node
the child node of a gridpane child, HPos the horizontal alignment for the child value)Sets the horizontal alignment for the child when contained by a gridpane. |
| public final void | |
| public static void | |
| public static void | |
| public static void | setRowIndex(Node
the child node of a gridpane child, Integer the row index of the child value)Sets the row index for the child when contained by a gridpane so that it will be positioned starting in that row of the gridpane. |
| public static void | setRowSpan(Node
the child node of a gridpane child, Integer the row span of the child value)Sets the row span for the child when contained by a gridpane so that it will span that number of rows vertically. |
| public static void | setValignment(Node
the child node of a gridpane child, VPos the vertical alignment for the child value)Sets the vertical alignment for the child when contained by a gridpane. |
| public final void | |
| public static void | |
| private boolean | |
| private boolean | |
| public String | Returns: a string representation of thisGridPane object.Overrides javafx. Returns a string representation of this |
| public final DoubleProperty | Returns: the height of the vertical gaps between rowsThe height of the vertical gaps between rows. |