Class Grid
- All Implemented Interfaces:
Proxy
,Accessible
,Buildable
,ConstraintTarget
,Orientable
GtkGrid
is a container which arranges its child widgets in
rows and columns.
It supports arbitrary positions and horizontal/vertical spans.
Children are added using attach(org.gnome.gtk.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(org.gnome.gtk.Widget, org.gnome.gtk.Widget, org.gnome.gtk.PositionType, int, int)
. To remove a child from the
grid, use remove(org.gnome.gtk.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_ACCESSIBLE_ROLE_GROUP
role.
Starting from GTK 4.12, GtkGrid
uses the GTK_ACCESSIBLE_ROLE_GENERIC
role.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Grid.Builder<B extends Grid.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gtk.Widget
Widget.DestroyCallback, Widget.DirectionChangedCallback, Widget.HideCallback, Widget.KeynavFailedCallback, Widget.MapCallback, Widget.MnemonicActivateCallback, Widget.MoveFocusCallback, Widget.QueryTooltipCallback, Widget.RealizeCallback, Widget.ShowCallback, Widget.StateFlagsChangedCallback, Widget.UnmapCallback, Widget.UnrealizeCallback, Widget.WidgetClass, Widget.WidgetImpl
Nested classes/interfaces inherited from class org.gnome.gobject.InitiallyUnowned
InitiallyUnowned.InitiallyUnownedClass
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
Nested classes/interfaces inherited from interface org.gnome.gtk.Accessible
Accessible.AccessibleImpl, Accessible.AccessibleInterface
Nested classes/interfaces inherited from interface org.gnome.gtk.Buildable
Buildable.BuildableIface, Buildable.BuildableImpl
Nested classes/interfaces inherited from interface org.gnome.gtk.ConstraintTarget
ConstraintTarget.ConstraintTargetImpl, ConstraintTarget.ConstraintTargetInterface
Nested classes/interfaces inherited from interface org.gnome.gtk.Orientable
Orientable.OrientableIface, Orientable.OrientableImpl
-
Constructor Summary
ConstructorDescriptionGrid()
Creates a new grid widget.Grid
(MemorySegment address) Create a Grid proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected Grid
asParent()
Returns this instance as if it were its parent type.void
Adds a widget to the grid.void
attachNextTo
(Widget child, @Nullable Widget sibling, PositionType side, int width, int height) Adds a widget to the grid.static Grid.Builder
<? extends Grid.Builder> builder()
AGrid.Builder
object constructs aGrid
with the specified properties.int
Returns which row defines the global baseline of this Grid.getChildAt
(int column, int row) Gets the child of this Grid whose area covers the grid cell atcolumn
,row
.boolean
Returns whether all columns of this Grid have the same width.int
Returns the amount of space between the columns of this Grid.static MemoryLayout
The memory layout of the native struct.getRowBaselinePosition
(int row) Returns the baseline position ofrow
.boolean
Returns whether all rows of this Grid have the same height.int
Returns the amount of space between the rows of this Grid.static Type
getType()
Get the GType of the Grid classvoid
insertColumn
(int position) Inserts a column at the specified position.void
insertNextTo
(Widget sibling, PositionType side) Inserts a row or column at the specified position.void
insertRow
(int position) Inserts a row at the specified position.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 ofchild
inside the givenGtkGrid
.void
Removes a child from this Grid.void
removeColumn
(int position) Removes a column from the grid.void
removeRow
(int position) Removes a row from the grid.void
setBaselineRow
(int row) Sets which row defines the global baseline for the entire grid.void
setColumnHomogeneous
(boolean homogeneous) Sets whether all columns of this Grid will have the same width.void
setColumnSpacing
(int spacing) Sets the amount of space between columns of this Grid.void
setRowBaselinePosition
(int row, BaselinePosition pos) Sets how the baseline should be positioned onrow
of the grid, in case that row is assigned more space than is requested.void
setRowHomogeneous
(boolean homogeneous) Sets whether all rows of this Grid will have the same height.void
setRowSpacing
(int spacing) Sets the amount of space between rows of this Grid.Methods inherited from class org.gnome.gtk.Widget
actionSetEnabled, activateActionIfExists, activateDefault, activateWidget, addController, addCssClass, addMnemonicLabel, addTickCallback, allocate, childFocus, computeBounds, computeExpand, computeExpand, computePoint, computeTransform, contains, createPangoContext, createPangoLayout, cssChanged, directionChanged, disposeTemplate, dragCheckThreshold, emitDestroy, emitDirectionChanged, emitHide, emitKeynavFailed, emitMap, emitMnemonicActivate, emitMoveFocus, emitQueryTooltip, emitRealize, emitShow, emitStateFlagsChanged, emitUnmap, emitUnrealize, errorBell, focus, getAllocatedBaseline, getAllocatedHeight, getAllocatedWidth, getAllocation, getAncestor, getBaseline, getCanFocus, getCanTarget, getChildVisible, getClipboard, getColor, getCssClasses, getCssName, getCursor, getDefaultDirection, getDirection, getDisplay, getFirstChild, getFocusable, getFocusChild, getFocusOnClick, getFontMap, getFontOptions, getFrameClock, getHalign, getHasTooltip, getHeight, getHexpand, getHexpandSet, getLastChild, getLayoutManager, getMapped, getMarginBottom, getMarginEnd, getMarginStart, getMarginTop, getName, getNative, getNextSibling, getOpacity, getOverflow, getPangoContext, getParent, getPreferredSize, getPrevSibling, getPrimaryClipboard, getRealized, getReceivesDefault, getRequestMode, getRoot, getScaleFactor, getSensitive, getSettings, getSize, getSizeRequest, getStateFlags, getStyleContext, getTemplateChild, getTooltipMarkup, getTooltipText, getValign, getVexpand, getVexpandSet, getVisible, getWidth, grabFocus, hasCssClass, hasDefault, hasFocus, hasVisibleFocus, hide, inDestruction, initTemplate, insertActionGroup, insertAfter, insertBefore, isAncestor, isDrawable, isFocus, isSensitive, isVisible, keynavFailed, listMnemonicLabels, map, measure, mnemonicActivate, moveFocus, observeChildren, observeControllers, onDestroy, onDirectionChanged, onHide, onKeynavFailed, onMap, onMnemonicActivate, onMoveFocus, onQueryTooltip, onRealize, onShow, onStateFlagsChanged, onUnmap, onUnrealize, pick, pick, queryTooltip, queueAllocate, queueDraw, queueResize, realize, removeController, removeCssClass, removeMnemonicLabel, removeTickCallback, root, setCanFocus, setCanTarget, setChildVisible, setCssClasses, setCursor, setCursorFromName, setDefaultDirection, setDirection, setFocusable, setFocusChild, setFocusOnClick, setFontMap, setFontOptions, setHalign, setHasTooltip, setHexpand, setHexpandSet, setLayoutManager, setMarginBottom, setMarginEnd, setMarginStart, setMarginTop, setName, setOpacity, setOverflow, setParent, setReceivesDefault, setSensitive, setSizeRequest, setStateFlags, setStateFlags, setTooltipMarkup, setTooltipText, setValign, setVexpand, setVexpandSet, setVisible, shouldLayout, show, sizeAllocate, sizeAllocate, snapshot, snapshotChild, stateFlagsChanged, systemSettingChanged, translateCoordinates, triggerTooltipQuery, unmap, unparent, unrealize, unroot, unsetStateFlags, unsetStateFlags
Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.gnome.gtk.Accessible
announce, getAccessibleParent, getAccessibleRole, getAtContext, getBounds, getFirstAccessibleChild, getNextAccessibleSibling, getPlatformState, resetProperty, resetRelation, resetState, setAccessibleParent, updateNextAccessibleSibling, updateProperty, updateRelation, updateState
Methods inherited from interface org.gnome.gtk.Buildable
getBuildableId
Methods inherited from interface org.gnome.gtk.Orientable
getOrientation, setOrientation
-
Constructor Details
-
Grid
Create a Grid proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Grid
public Grid()Creates a new grid widget.
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
attach
Adds a widget to the grid.The position of
child
is determined bycolumn
androw
. The number of “cells” thatchild
will occupy is determined bywidth
andheight
.- Parameters:
child
- the widget to addcolumn
- the column number to attach the left side ofchild
torow
- the row number to attach the top side ofchild
towidth
- the number of columns thatchild
will spanheight
- the number of rows thatchild
will span
-
attachNextTo
public void attachNextTo(Widget child, @Nullable @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 byside
. Whensibling
isnull
, the widget is placed in row (for left or right placement) or column 0 (for top or bottom placement), at the end indicated byside
.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 addsibling
- the child of this Grid thatchild
will be placed next to, ornull
to placechild
at the beginning or endside
- the side ofsibling
thatchild
is positioned next towidth
- the number of columns thatchild
will spanheight
- the number of rows thatchild
will span
-
getBaselineRow
public int getBaselineRow()Returns which row defines the global baseline of this Grid.- Returns:
- the row index defining the global baseline
-
getChildAt
Gets the child of this Grid whose area covers the grid cell atcolumn
,row
.- Parameters:
column
- the left edge of the cellrow
- 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 this Grid.- Returns:
- the column spacing of this Grid
-
getRowBaselinePosition
Returns the baseline position ofrow
.See
setRowBaselinePosition(int, org.gnome.gtk.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 this 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
Inserts a row or column at the specified position.The new row or column is placed next to
sibling
, on the side determined byside
. Ifside
isPositionType.TOP
orPositionType.BOTTOM
, a row is inserted. Ifside
isPositionType.LEFT
ofPositionType.RIGHT
, a column is inserted.- Parameters:
sibling
- the child of this Grid that the new row or column will be placed next toside
- the side ofsibling
thatchild
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 @Nullable Out<Integer> column, @Nullable @Nullable Out<Integer> row, @Nullable @Nullable Out<Integer> width, @Nullable @Nullable Out<Integer> height) Queries the attach points and spans ofchild
inside the givenGtkGrid
.- Parameters:
child
- aGtkWidget
child of this Gridcolumn
- the column used to attach the left side ofchild
row
- the row used to attach the top side ofchild
width
- the number of columnschild
spansheight
- the number of rowschild
spans
-
remove
Removes a child from this Grid.The child must have been added with
attach(org.gnome.gtk.Widget, int, int, int, int)
orattachNextTo(org.gnome.gtk.Widget, org.gnome.gtk.Widget, org.gnome.gtk.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 this 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 this Grid.- Parameters:
spacing
- the amount of space to insert between columns
-
setRowBaselinePosition
Sets how the baseline should be positioned onrow
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 indexpos
- aGtkBaselinePosition
-
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 this Grid.- Parameters:
spacing
- the amount of space to insert between rows
-
builder
AGrid.Builder
object constructs aGrid
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withGrid.Builder.build()
.
-