Class Grid

All Implemented Interfaces:
Accessible, Buildable, ConstraintTarget, Orientable, Proxy

@Generated("org.javagi.JavaGI") public class Grid extends Widget implements Accessible, Buildable, ConstraintTarget, Orientable

Arranges its child widgets in rows and columns.

An example GtkGrid

It supports arbitrary positions and horizontal/vertical spans.

Children are added using attach(Widget, int, int, int, int). They can span multiple rows or columns. It is also possible to add a child next to an existing child, using attachNextTo(Widget, Widget, PositionType, int, int). To remove a child from the grid, use remove(Widget).

The behaviour of GtkGrid when several children occupy the same grid cell is undefined.

GtkGrid as GtkBuildable

Every child in a GtkGrid has access to a custom Buildable element, called <layout>. It can by used to specify a position in the grid and optionally spans. All properties that can be used in the <layout> element are implemented by GridLayoutChild.

It is implemented by GtkWidget using LayoutManager.

To showcase it, here is a simple example:

<object class="GtkGrid" id="my_grid">
  <child>
    <object class="GtkButton" id="button1">
      <property name="label">Button 1</property>
      <layout>
        <property name="column">0</property>
        <property name="row">0</property>
      </layout>
    </object>
  </child>
  <child>
    <object class="GtkButton" id="button2">
      <property name="label">Button 2</property>
      <layout>
        <property name="column">1</property>
        <property name="row">0</property>
      </layout>
    </object>
  </child>
  <child>
    <object class="GtkButton" id="button3">
      <property name="label">Button 3</property>
      <layout>
        <property name="column">2</property>
        <property name="row">0</property>
        <property name="row-span">2</property>
      </layout>
    </object>
  </child>
  <child>
    <object class="GtkButton" id="button4">
      <property name="label">Button 4</property>
      <layout>
        <property name="column">0</property>
        <property name="row">1</property>
        <property name="column-span">2</property>
      </layout>
    </object>
  </child>
</object>

It organizes the first two buttons side-by-side in one cell each. The third button is in the last column but spans across two rows. This is defined by the row-span property. The last button is located in the second row and spans across two columns, which is defined by the column-span property.

CSS nodes

GtkGrid uses a single CSS node with name grid.

Accessibility

Until GTK 4.10, GtkGrid used the Gtk.AccessibleRole.group role.

Starting from GTK 4.12, GtkGrid uses the Gtk.AccessibleRole.generic role.

  • Constructor Details

    • Grid

      public Grid(MemorySegment address)
      Create a Grid instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Grid

      public Grid()
      Create a new Grid.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Grid class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Grid asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Widget
      Returns:
      the instance as if it were its parent type
    • attach

      public void attach(Widget child, int column, int row, int width, int height)

      Adds a widget to the grid.

      The position of child is determined by column and row. The number of “cells” that child will occupy is determined by width and height.

      Parameters:
      child - the widget to add
      column - the column number to attach the left side of child to
      row - the row number to attach the top side of child to
      width - the number of columns that child will span
      height - the number of rows that child will span
    • attachNextTo

      public void attachNextTo(Widget child, @Nullable Widget sibling, PositionType side, int width, int height)

      Adds a widget to the grid.

      The widget is placed next to sibling, on the side determined by side. When sibling is null, the widget is placed in row (for left or right placement) or column 0 (for top or bottom placement), at the end indicated by side.

      Attaching widgets labeled [1], [2], [3] with @sibling == %NULL and @side == %GTK_POS_LEFT yields a layout of [3][2][1].

      Parameters:
      child - the widget to add
      sibling - the child of this Grid that child will be placed next to, or null to place child at the beginning or end
      side - the side of sibling that child is positioned next to
      width - the number of columns that child will span
      height - the number of rows that child will span
    • getBaselineRow

      public int getBaselineRow()
      Returns which row defines the global baseline of grid.
      Returns:
      the row index defining the global baseline
    • getChildAt

      public @Nullable Widget getChildAt(int column, int row)
      Gets the child of this Grid whose area covers the grid cell at column, row.
      Parameters:
      column - the left edge of the cell
      row - the top edge of the cell
      Returns:
      the child at the given position
    • getColumnHomogeneous

      public boolean getColumnHomogeneous()
      Returns whether all columns of this Grid have the same width.
      Returns:
      whether all columns of this Grid have the same width.
    • getColumnSpacing

      public int getColumnSpacing()
      Returns the amount of space between the columns of grid.
      Returns:
      the column spacing of this Grid
    • getRowBaselinePosition

      public BaselinePosition getRowBaselinePosition(int row)

      Returns the baseline position of row.

      See setRowBaselinePosition(int, BaselinePosition).

      Parameters:
      row - a row index
      Returns:
      the baseline position of row
    • getRowHomogeneous

      public boolean getRowHomogeneous()
      Returns whether all rows of this Grid have the same height.
      Returns:
      whether all rows of this Grid have the same height.
    • getRowSpacing

      public int getRowSpacing()
      Returns the amount of space between the rows of grid.
      Returns:
      the row spacing of this Grid
    • insertColumn

      public void insertColumn(int position)

      Inserts a column at the specified position.

      Children which are attached at or to the right of this position are moved one column to the right. Children which span across this position are grown to span the new column.

      Parameters:
      position - the position to insert the column at
    • insertNextTo

      public void insertNextTo(Widget sibling, PositionType side)

      Inserts a row or column at the specified position.

      The new row or column is placed next to sibling, on the side determined by side. If side is PositionType.TOP or PositionType.BOTTOM, a row is inserted. If side is PositionType.LEFT of PositionType.RIGHT, a column is inserted.

      Parameters:
      sibling - the child of this Grid that the new row or column will be placed next to
      side - the side of sibling that child is positioned next to
    • insertRow

      public void insertRow(int position)

      Inserts a row at the specified position.

      Children which are attached at or below this position are moved one row down. Children which span across this position are grown to span the new row.

      Parameters:
      position - the position to insert the row at
    • queryChild

      public void queryChild(Widget child, @Nullable Out<Integer> column, @Nullable Out<Integer> row, @Nullable Out<Integer> width, @Nullable Out<Integer> height)
      Queries the attach points and spans of child inside the given GtkGrid.
      Parameters:
      child - a GtkWidget child of this Grid
      column - the column used to attach the left side of child
      row - the row used to attach the top side of child
      width - the number of columns child spans
      height - the number of rows child spans
    • remove

      public void remove(Widget child)

      Removes a child from grid.

      The child must have been added with attach(Widget, int, int, int, int) or attachNextTo(Widget, Widget, PositionType, int, int).

      Parameters:
      child - the child widget to remove
    • removeColumn

      public void removeColumn(int position)

      Removes a column from the grid.

      Children that are placed in this column are removed, spanning children that overlap this column have their width reduced by one, and children after the column are moved to the left.

      Parameters:
      position - the position of the column to remove
    • removeRow

      public void removeRow(int position)

      Removes a row from the grid.

      Children that are placed in this row are removed, spanning children that overlap this row have their height reduced by one, and children below the row are moved up.

      Parameters:
      position - the position of the row to remove
    • setBaselineRow

      public void setBaselineRow(int row)

      Sets which row defines the global baseline for the entire grid.

      Each row in the grid can have its own local baseline, but only one of those is global, meaning it will be the baseline in the parent of the grid.

      Parameters:
      row - the row index
    • setColumnHomogeneous

      public void setColumnHomogeneous(boolean homogeneous)
      Sets whether all columns of this Grid will have the same width.
      Parameters:
      homogeneous - true to make columns homogeneous
    • setColumnSpacing

      public void setColumnSpacing(int spacing)
      Sets the amount of space between columns of grid.
      Parameters:
      spacing - the amount of space to insert between columns
    • setRowBaselinePosition

      public void setRowBaselinePosition(int row, BaselinePosition pos)

      Sets how the baseline should be positioned on row of the grid, in case that row is assigned more space than is requested.

      The default baseline position is BaselinePosition.CENTER.

      Parameters:
      row - a row index
      pos - a GtkBaselinePosition
    • setRowHomogeneous

      public void setRowHomogeneous(boolean homogeneous)
      Sets whether all rows of this Grid will have the same height.
      Parameters:
      homogeneous - true to make rows homogeneous
    • setRowSpacing

      public void setRowSpacing(int spacing)
      Sets the amount of space between rows of grid.
      Parameters:
      spacing - the amount of space to insert between rows
    • builder

      public static Grid.Builder<? extends Grid.Builder> builder()
      A Grid.Builder object constructs a Grid with the specified properties. Use the various set...() methods to set properties, and finish construction with Grid.Builder.build().
      Returns:
      the builder object