Skip to content

Grid layout

Inventories, galleries and color palettes show many items in rows that wrap. A GridNode fills a row from left to right, starts the next row when the width is full, and grows to hold its rows.

You will learn

  • How to fill rows of cells that wrap at the width of the grid
  • How to space the cells on each axis
  • How to align each row, and how rows of different heights stack
  • How a grid takes its height

GridNode.create takes a position and a size; its children are its cells:

GridNode
.create(20, 20, 360, 0)
.margin(10D)
.body(grid -> {
for (int i = 0; i < 9; i++) {
RectNode.create(0, 0, 80, 80).color(GridCasesUI.ACCENT).attach(grid);
}
})
.attach(card);

Full file: GridCasesUI.java

On a dark card, nine blue squares: two full rows of four, then a single square on a third row, all aligned to the left

Four squares and their gaps fit in 360 design pixels: the fifth starts the second row.

A cell goes on the current row while the row, gaps included, stays within the width of the grid; otherwise it starts a new row. A cell wider than the grid gets a row of its own. The cells keep their own size, and take their places in the order you attach them, again on every frame: the grid is a layout node. A hidden, removed or ABSOLUTE child gets no cell, and the next cells move up from the next frame.

GridNode.margin sets the same gap on both axes. GridNode.marginHorizontal sets the gap between two cells of a row, GridNode.marginVertical the gap between two rows:

GridNode
.create(20, 20, 360, 0)
.marginHorizontal(20D)
.marginVertical(5D)
.body(grid -> {
for (int i = 0; i < 8; i++) {
RectNode.create(0, 0, 70, 70).color(GridCasesUI.ACCENT).attach(grid);
}
})
.attach(card);

Full file: GridCasesUI.java

On a dark card, two rows of four blue squares, with wide gaps between the squares of a row and a thin gap between the rows

20 design pixels between the cells of a row, 5 between the rows.

There is no gap before the first cell nor after the last one of a row.

GridNode.alignX takes an Align and places each row on the width of the grid, on its own width:

GridNode
.create(20, 20, 360, 0)
.margin(10D)
.alignX(Align.CENTER)
.body(grid -> {
for (int i = 0; i < 6; i++) {
RectNode.create(0, 0, 80, 80).color(GridCasesUI.ACCENT).attach(grid);
}
})
.attach(card);

Full file: GridCasesUI.java

On a dark card, a full row of four blue squares, and below it a row of two squares centered under them

Align.CENTER centers the full row and the short last row each on its own width.

Align.START, the default, puts every row against the left edge, and Align.END against the right edge. The own x and y of a cell add to its place after the alignment, as in every layout node.

A row is as high as its tallest cell, and the next row starts below it:

GridNode
.create(20, 20, 360, 0)
.margin(10D)
.body(grid -> {
RectNode.create(0, 0, 110, 60).color(GridCasesUI.ELEMENT).attach(grid);
RectNode.create(0, 0, 110, 120).color(GridCasesUI.ACCENT).attach(grid);
RectNode.create(0, 0, 110, 80).color(GridCasesUI.ELEMENT).attach(grid);
RectNode.create(0, 0, 170, 60).color(GridCasesUI.ELEMENT).attach(grid);
RectNode.create(0, 0, 170, 60).color(GridCasesUI.ELEMENT).attach(grid);
})
.attach(card);

Full file: GridCasesUI.java

On a dark card, a first row of three cells of the same width and different heights, the blue one tallest, then a second row of two wider dark grey cells that starts below the blue cell

The blue cell sets the height of the first row; the cells of a row are aligned at its top.

A grid created with a height of 0 takes the height of its rows, gaps included, and keeps the width you gave it.

The width of this grid changes over time: each square moves to the row where it fits, and the height of the grid follows its rows.

A height that you give follows the rule of the layout nodes: with an overflow other than NONE, it stays fixed and the rows scroll inside it.

  • GridNode.create(x, y, width, height) places its children in rows, in attach order, and wraps at its width.
  • margin spaces the cells on both axes; marginHorizontal and marginVertical on one axis.
  • alignX places each row on the width of the grid.
  • A row is as high as its tallest cell.
  • A height of 0 follows the rows; a declared height follows the rule of the layout nodes.

A palette. Build a grid of 16 blue squares of 40 design pixels, four per row, 8 design pixels apart.

Solution

Four squares and three gaps take 4 × 40 + 3 × 8 = 184 design pixels: a grid of that width wraps after the fourth square.

GridNode
.create(20, 20, 184, 0)
.margin(8D)
.body(grid -> {
for (int i = 0; i < 16; i++) {
RectNode.create(0, 0, 40, 40).color(PaletteUI.ACCENT).attach(grid);
}
})
.attach(this);

Full file: PaletteUI.java

Right to left. Make the last row of the align example sit against the right edge.

Solution

Replace Align.CENTER by Align.END: each row goes against the right edge, the full row included.

Report a problem with this page