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
Fill rows that wrap
Section titled “Fill rows that wrap”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);
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.
Space the cells
Section titled “Space the cells”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);
There is no gap before the first cell nor after the last one of a row.
Align each row
Section titled “Align each 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);
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.
Mix cells of different sizes
Section titled “Mix cells of different sizes”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);
Let the grid take its height
Section titled “Let the grid take its height”A grid created with a height of 0 takes the height of its rows, gaps included, and keeps the width you gave it.
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.marginspaces the cells on both axes;marginHorizontalandmarginVerticalon one axis.alignXplaces 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.
Try it
Section titled “Try it”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);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.
