Top Description Inners Fields Constructors Methods
javafx.scene.layout

public Class GridPane

extends Pane
Class Inheritance
Imports
java.util.ArrayList, .Collections, .List, .Arrays, .BitSet, .Iterator, .Map.Entry, .Set, .SortedMap, .TreeMap, .TreeSet, javafx.beans.property.BooleanProperty, .DoubleProperty, .ObjectProperty, javafx.collections.ListChangeListener.Change, .ObservableList, javafx.geometry.HPos, .Insets, .Orientation, .Pos, .VPos, .BoundingBox, .Bounds, javafx.scene.Group, .Node, javafx.scene.paint.Color, javafx.scene.shape.Line, com.sun.javafx.collections.TrackableObservableList, javafx.css.StyleableBooleanProperty, .StyleableDoubleProperty, .StyleableObjectProperty, .CssMetaData, .Styleable, .StyleableProperty, javafx.css.converter.BooleanConverter, .EnumConverter, .SizeConverter, javafx.beans.Observable, javafx.util.Callback

GridPane lays out its children within a flexible grid of rows and columns. If a border and/or padding is set, then its content will be laid out within those insets.

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.

Grid Constraints

A child's placement within the grid is defined by it's layout constraints:

Grid Constraint Table
ConstraintTypeDescription
columnIndexintegercolumn where child's layout area starts.
rowIndexintegerrow where child's layout area starts.
columnSpanintegerthe number of columns the child's layout area spans horizontally.
rowSpanintegerthe 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

Row/Column Sizing

By default, rows and columns will be sized to fit their content; a column will be wide enough to accommodate the widest child, a row tall enough to fit the tallest child. However, if an application needs to explicitly control the size of rows or columns, it may do so by adding RowConstraints and ColumnConstraints objects to specify those metrics. For example, to create a grid with two fixed-width columns:

    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.

Percentage Sizing

Alternatively, RowConstraints and ColumnConstraints allow the size to be specified as a percentage of gridpane's available space:

    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)).

Mixing Size Types

An application may freely mix the size-types of rows/columns (computed from content, fixed, or percentage). The percentage rows/columns will always be allocated space first based on their percentage of the gridpane's available space (size minus insets and gaps). The remaining space will be allocated to rows/columns given their minimum, preferred, and maximum sizes and grow priorities.

Resizable Range

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.

GridPane Resize Table
widthheight
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_VALUEDouble.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.

Optional Layout Constraints

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:

GridPane Constraint Table
ConstraintTypeDescription
halignmentjavafx.geometry.HPosThe horizontal alignment of the child within its layout area.
valignmentjavafx.geometry.VPosThe vertical alignment of the child within its layout area.
hgrowjavafx.scene.layout.PriorityThe horizontal grow priority of the child.
vgrowjavafx.scene.layout.PriorityThe vertical grow priority of the child.
marginjavafx.geometry.InsetsMargin 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.

Since
JavaFX 2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static class

Field Summary

Modifier and TypeField and Description
private ObjectProperty<Pos>
private Orientation
private static final String
private static final String
private final ObservableList<ColumnConstraints>
columnConstraints

ColumnConstraints instances can be added to explicitly control individual column sizing and layout behavior.

private Priority[]
private GridPane.CompositeSize
private GridPane.CompositeSize
private double
private double[]
private GridPane.CompositeSize
private GridPane.CompositeSize
private GridPane.CompositeSize
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<Node, Insets>
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<Node>[]
private final ObservableList<RowConstraints>
rowConstraints

RowConstraints instances can be added to explicitly control individual row sizing and layout behavior.

private Priority[]
private double[]
private GridPane.CompositeSize
private double[]
private GridPane.CompositeSize
private double[]
private double
private double[]
private GridPane.CompositeSize
private static final String
private DoubleProperty
private static final String

Constructor Summary

AccessConstructor and Description
public
GridPane()

Creates a GridPane layout with hgap/vgap = 0 and TOP_LEFT alignment.

public
GridPane(double
the size of the horizontal gaps between columns
hgap
,
double
the size of the vertical gaps between rows
vgap
)

Creates a GridPane layout with the given hgap and vgap.

Method Summary

Modifier and TypeMethod 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
addColumn(int
the column index position for the children within the gridpane
columnIndex
,
Node...
the nodes to be added as a column in the gridpane
children
)

Convenience method for placing the specified nodes sequentially in a given column of the gridpane.

public void
addRow(int
the row index position for the children within the gridpane
rowIndex
,
Node...
the nodes to be added as a row in the gridpane
children
)

Convenience method for placing the specified nodes sequentially in a given row of the gridpane.

private double
adjustColumnWidths(final GridPane.CompositeSize widths, double width)

private double
adjustRowHeights(final GridPane.CompositeSize heights, double height)

public final ObjectProperty<Pos>

Returns:

the alignment of the grid within the gridpane's width and height
alignmentProperty
()

The alignment of the grid within the gridpane's width and height.

public static void
clearConstraints(Node
the child node
child
)

Removes all gridpane constraints from the child node.

private void
private GridPane.CompositeSize
computeHeightsToFit(double height)

private GridPane.CompositeSize
private GridPane.CompositeSize
protected double
computeMinHeight(double
the width that should be used if min height depends on it
width
)

Overrides javafx.scene.layout.Region.computeMinHeight.

Computes the minimum height of this region.

private GridPane.CompositeSize
computeMinHeights(double[] widths)

protected double
computeMinWidth(double
the height that should be used if min width depends on it
height
)

Overrides javafx.scene.layout.Region.computeMinWidth.

Computes the minimum width of this region.

private GridPane.CompositeSize
computeMinWidths(double[] heights)

protected double
computePrefHeight(double
the width that should be used if preferred height depends on it
width
)

Overrides javafx.scene.layout.Region.computePrefHeight.

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.CompositeSize
computePrefHeights(double[] widths)

protected double
computePrefWidth(double
the height that should be used if preferred width depends on it
height
)

Overrides javafx.scene.layout.Region.computePrefWidth.

Computes the preferred width of this region for the given height.

private GridPane.CompositeSize
computePrefWidths(double[] heights)

private GridPane.CompositeSize
computeWidthsToFit(double width)

pack-priv static void
createColumn(int columnIndex, int rowIndex, Node... nodes)

private GridPane.CompositeSize
createCompositeColumns(double initSize)

private GridPane.CompositeSize
createCompositeRows(double initSize)

private Line
createGridLine(double startX, double startY, double endX, double endY)

private static Priority[]
createPriorityArray(int length, Priority value)

pack-priv static void
createRow(int rowIndex, int columnIndex, Node... nodes)

public final Pos
private Pos
private double
public final Bounds

Returns:

the bounds of the cell at columnIndex and rowIndex.
getCellBounds
(int
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<CssMetaData<? extends Styleable, ?>>

Returns:

the CssMetaData
getClassCssMetaData
()

Hides javafx.scene.layout.Region.getClassCssMetaData.

Gets the CssMetaData associated with this class, which may include the CssMetaData of its superclasses.

public final ObservableList<ColumnConstraints>

Returns:

the list of column constraints
getColumnConstraints
()

Returns list of column constraints.

public final int

Returns:

the column count
getColumnCount
()

Returns the number of columns in this GridPane.

private HPos
getColumnHalignment(int columnIndex)

public static Integer

Returns:

the column index for the child or null if no column index was set
getColumnIndex
(Node
the child node of a gridpane
child
)

Returns the child's column index constraint if set.

private double
getColumnMaxWidth(int columnIndex)

private double
getColumnMinWidth(int columnIndex)

private double
getColumnPrefWidth(int columnIndex)

public static Integer

Returns:

the column span for the child or null if no column span was set
getColumnSpan
(Node
the 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.
getContentBias
()

Overrides javafx.scene.Node.getContentBias.

Returns the orientation of a node's resizing bias for layout purposes.

public List<CssMetaData<? extends Styleable, ?>>
getCssMetaData()

Overrides javafx.scene.layout.Region.getCssMetaData.

Implements javafx.css.Styleable.getCssMetaData.

This method should delegate to Node#getClassCssMetaData() so that a Node's CssMetaData can be accessed without the need for reflection.

pack-priv double[][]
public static HPos

Returns:

the horizontal alignment for the child or null if no alignment was set
getHalignment
(Node
the child node of a gridpane
child
)

Returns the child's halignment constraint if set.

public final double
public static Priority

Returns:

the horizontal grow priority for the child or null if no priority was set
getHgrow
(Node
the child node of a gridpane
child
)

Returns the child's hgrow constraint if set.

public static Insets

Returns:

the margin for the child or null if no margin was set
getMargin
(Node
the child node of a gridpane
child
)

Returns the child's margin constraint if set.

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<RowConstraints>

Returns:

the list of row constraints
getRowConstraints
()

Returns list of row constraints.

public final int

Returns:

the row count
getRowCount
()

Returns the number of rows in this GridPane.

public static Integer

Returns:

the row index for the child or null if no row index was set
getRowIndex
(Node
the child node of a gridpane
child
)

Returns the child's row index constraint if set.

private double
getRowMaxHeight(int rowIndex)

private double
getRowMinHeight(int rowIndex)

private double
getRowPrefHeight(int rowIndex)

public static Integer

Returns:

the row span for the child or null if no row span was set
getRowSpan
(Node
the child node of a gridpane
child
)

Returns the child's row-span constraint if set.

private VPos
getRowValignment(int rowIndex)

private double
getTotalHeightOfNodeRows(Node child, double[] heights)

private double
getTotalWidthOfNodeColumns(Node child, double[] widths)

public static VPos

Returns:

the vertical alignment for the child or null if no alignment was set
getValignment
(Node
the child node of a gridpane
child
)

Returns the child's valignment constraint if set.

public final double
public static Priority

Returns:

the vertical grow priority for the child or null if no priority was set
getVgrow
(Node
the child node of a gridpane
child
)

Returns the child's vgrow constraint if set.

public final BooleanProperty

Returns:

true if lines are displayed to show the gridpane's rows and columns
gridLinesVisibleProperty
()

For debug purposes only: controls whether lines are displayed to show the gridpane's rows and columns.

private double
growOrShrinkColumnWidths(GridPane.CompositeSize widths, Priority priority, double extraWidth)

private double
growOrShrinkRowHeights(GridPane.CompositeSize heights, Priority priority, double extraHeight)

private double
private double
public final DoubleProperty

Returns:

the width of the horizontal gaps between columns
hgapProperty
()

The width of the horizontal gaps between columns.

public static Boolean

Returns:

the vertical fill policy for the child or null if no policy was set
isFillHeight
(Node
the 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 set
isFillWidth
(Node
the 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.scene.Parent.layoutChildren.

Invoked during the layout pass to layout the children in this Parent.

private void
layoutGridLines(GridPane.CompositeSize columnWidths, GridPane.CompositeSize rowHeights, double x, double y, double columnHeight, double rowWidth)

public void
requestLayout()

Overrides javafx.scene.Parent.requestLayout.

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
setGridLinesVisible(boolean value)

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
setHgap(double value)

public static void
setHgrow(Node
the child of a gridpane
child
,
Priority
the horizontal grow priority for the child
value
)

Sets the horizontal grow priority for the child when contained by a gridpane.

public static void
setMargin(Node
the child node of a gridpane
child
,
Insets
the margin of space around the child
value
)

Sets the margin for the child when contained by a gridpane.

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
setVgap(double value)

public static void
setVgrow(Node
the child of a gridpane
child
,
Priority
the vertical grow priority for the child
value
)

Sets the vertical grow priority for the child when contained by a gridpane.

private boolean
shouldColumnFillWidth(int columnIndex)

private boolean
shouldRowFillHeight(int rowIndex)

public String

Returns:

a string representation of this GridPane object.
toString
()

Overrides javafx.scene.Node.toString.

Returns a string representation of this GridPane object.

public final DoubleProperty

Returns:

the height of the vertical gaps between rows
vgapProperty
()

The height of the vertical gaps between rows.

Inherited from javafx.scene.layout.Pane:
getChildrengetConstraintsetConstraint