public class GridLayout extends AbstractLayout implements Layout.AlignmentHandler, Layout.SpacingHandler, Layout.MarginHandler, LayoutEvents.LayoutClickNotifier
The GridLayout also maintains a cursor for adding components in left-to-right, top-to-bottom order.
Each component in a GridLayout uses a defined
area (column1,row1,column2,row2) from the grid. The
components may not overlap with the existing components - if you try to do so
you will get an GridLayout.OverlapsException. Adding a component with cursor
automatically extends the grid by increasing the grid height.
The grid coordinates, which are specified by a row and column index, always start from 0 for the topmost row and the leftmost column.
| Modifier and Type | Class and Description |
|---|---|
class |
GridLayout.Area
Defines a rectangular area of cells in a GridLayout.
|
class |
GridLayout.OutOfBoundsException
An
Exception object which is thrown when an area exceeds the
bounds of the grid. |
class |
GridLayout.OverlapsException
Gridlayout does not support laying components on top of each other.
|
Layout.AlignmentHandler, Layout.MarginHandler, Layout.SpacingHandlerHasComponents.ComponentAttachDetachNotifier, HasComponents.ComponentAttachEvent, HasComponents.ComponentAttachListener, HasComponents.ComponentDetachEvent, HasComponents.ComponentDetachListenerComponent.ErrorEvent, Component.Event, Component.Focusable, Component.ListenerClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListenerSizeable.UnitDESIGN_ATTR_PLAIN_TEXTSIZE_UNDEFINED, UNITS_CM, UNITS_EM, UNITS_EX, UNITS_INCH, UNITS_MM, UNITS_PERCENTAGE, UNITS_PICAS, UNITS_PIXELS, UNITS_POINTS| Constructor and Description |
|---|
GridLayout()
Constructs an empty (1x1) grid layout that is extended as needed.
|
GridLayout(int columns,
int rows)
Constructor for a grid of given size (number of columns and rows).
|
GridLayout(int columns,
int rows,
Component... children)
Constructs a GridLayout of given size (number of columns and rows) and
adds the given components in order to the grid.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addComponent(Component component)
Adds the component into this container to the cursor position.
|
void |
addComponent(Component component,
int column,
int row)
Adds the component to the grid in cells column1,row1 (NortWest corner of
the area.) End coordinates (SouthEast corner of the area) are the same as
column1,row1.
|
void |
addComponent(Component component,
int column1,
int row1,
int column2,
int row2)
Adds a component to the grid in the specified area.
|
void |
addLayoutClickListener(LayoutEvents.LayoutClickListener listener)
Add a click listener to the layout.
|
void |
addListener(LayoutEvents.LayoutClickListener listener)
Deprecated.
As of 7.0, replaced by
#addLayoutClickListener(LayoutClickListener) |
void |
beforeClientResponse(boolean initial)
Called before the shared state and RPC invocations are sent to the
client.
|
float |
getColumnExpandRatio(int columnIndex)
Returns the expand ratio of given column
|
int |
getColumns()
Get the number of columns in the grid.
|
Component |
getComponent(int x,
int y)
Gets the Component at given index.
|
Alignment |
getComponentAlignment(Component childComponent)
Returns the current Alignment of given component.
|
GridLayout.Area |
getComponentArea(Component component)
Returns information about the area where given component is laid in the
GridLayout.
|
int |
getComponentCount()
Gets the number of components contained in the layout.
|
int |
getCursorX()
Gets the current x-position (column) of the cursor.
|
int |
getCursorY()
Gets the current y-position (row) of the cursor.
|
protected Collection<String> |
getCustomAttributes()
Returns a collection of attributes that should not be handled by the
basic implementation of the
readDesign and writeDesign
methods. |
Alignment |
getDefaultComponentAlignment()
Returns the alignment used for new components added to this layout
|
MarginInfo |
getMargin() |
float |
getRowExpandRatio(int rowIndex)
Returns the expand ratio of given row.
|
int |
getRows()
Get the number of rows in the grid.
|
protected GridLayoutState |
getState()
Returns the shared state bean with information to be sent from the server
to the client.
|
protected GridLayoutState |
getState(boolean markAsDirty)
Returns the shared state for this connector.
|
void |
insertRow(int row)
Inserts an empty row at the specified position in the grid.
|
boolean |
isHideEmptyRowsAndColumns()
Checks whether whether empty rows and columns should be considered as
non-existent when rendering or not.
|
boolean |
isSpacing() |
Iterator<Component> |
iterator()
Gets an Iterator for the components contained in the layout.
|
void |
newLine()
Forces the next component to be added at the beginning of the next line.
|
void |
readDesign(org.jsoup.nodes.Element design,
DesignContext designContext)
Reads the component state from the given design.
|
void |
removeAllComponents()
Removes all components from the container.
|
void |
removeComponent(Component component)
Removes the specified component from the layout.
|
void |
removeComponent(int column,
int row)
Removes the component specified by its cell coordinates.
|
void |
removeLayoutClickListener(LayoutEvents.LayoutClickListener listener)
Removes an LayoutClickListener.
|
void |
removeListener(LayoutEvents.LayoutClickListener listener)
Deprecated.
As of 7.0, replaced by
#removeLayoutClickListener(LayoutClickListener) |
void |
removeRow(int row)
Removes a row and all the components in the row.
|
void |
replaceComponent(Component oldComponent,
Component newComponent)
Replaces the component in the container with another one without changing
position.
|
void |
setColumnExpandRatio(int columnIndex,
float ratio)
Sets the expand ratio of given column.
|
void |
setColumns(int columns)
Sets the number of columns in the grid.
|
void |
setComponentAlignment(Component childComponent,
Alignment alignment)
Set alignment for one contained component in this layout.
|
void |
setCursorX(int cursorX)
Sets the current cursor x-position.
|
void |
setCursorY(int cursorY)
Sets the current y-coordinate (row) of the cursor.
|
void |
setDefaultComponentAlignment(Alignment defaultAlignment)
Sets the alignment used for new components added to this layout.
|
void |
setHideEmptyRowsAndColumns(boolean hideEmptyRowsAndColumns)
Sets whether empty rows and columns should be considered as non-existent
when rendering or not.
|
void |
setMargin(boolean enabled)
Enable layout margins.
|
void |
setMargin(MarginInfo marginInfo)
Enable margins for this layout.
|
void |
setRowExpandRatio(int rowIndex,
float ratio)
Sets the expand ratio of given row.
|
void |
setRows(int rows)
Sets the number of rows in the grid.
|
void |
setSpacing(boolean spacing)
Enable spacing between child components within this layout.
|
void |
space()
Moves the cursor forward by one.
|
void |
writeDesign(org.jsoup.nodes.Element design,
DesignContext designContext)
Writes the component state to the given design.
|
readMargin, writeMarginaddComponentAttachListener, addComponentDetachListener, addComponents, addListener, addListener, fireComponentAttachEvent, fireComponentDetachEvent, getComponentIterator, moveComponentsFrom, removeComponentAttachListener, removeComponentDetachListener, removeListener, removeListener, setHeight, setWidthaddContextClickListener, addListener, addShortcutListener, addStyleName, attach, detach, findAncestor, fireComponentErrorEvent, fireComponentEvent, focus, getActionManager, getCaption, getComponentError, getData, getDebugId, getDescription, getErrorMessage, getExplicitImmediateValue, getHeight, getHeightUnits, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getWidth, getWidthUnits, isCaptionAsHtml, isConnectorEnabled, isEnabled, isImmediate, isOrHasAncestor, isReadOnly, isResponsive, isVisible, removeContextClickListener, removeListener, removeShortcutListener, removeStyleName, setCaption, setCaptionAsHtml, setComponentError, setData, setDebugId, setDescription, setEnabled, setHeight, setHeightUndefined, setIcon, setId, setImmediate, setLocale, setParent, setPrimaryStyleName, setReadOnly, setResponsive, setSizeFull, setSizeUndefined, setStyleName, setStyleName, setVisible, setWidth, setWidthUndefinedaddAttachListener, addDetachListener, addExtension, addListener, addListener, addListener, addMethodInvocationToQueue, createState, encodeState, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getStateType, getUI, handleConnectorRequest, hashCode, hasListeners, isAttached, isThis, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResourceclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitaddComponents, addListener, addListener, getComponentIterator, moveComponentsFrom, removeListener, removeListeneraddListener, addStyleName, attach, getCaption, getDescription, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getUI, isEnabled, isReadOnly, isVisible, removeListener, removeStyleName, setCaption, setEnabled, setIcon, setId, setParent, setPrimaryStyleName, setReadOnly, setStyleName, setVisibleaddAttachListener, addDetachListener, detach, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, handleConnectorRequest, isAttached, isConnectorEnabled, markAsDirty, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandlergetConnectorIdgetHeight, getHeightUnits, getWidth, getWidthUnits, setHeight, setHeight, setHeightUndefined, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthUndefinedforEach, spliteratoraddComponentAttachListener, addComponentDetachListener, removeComponentAttachListener, removeComponentDetachListenerpublic GridLayout(int columns,
int rows)
columns - Number of columns in the grid.rows - Number of rows in the grid.public GridLayout()
public GridLayout(int columns,
int rows,
Component... children)
columns - Number of columns in the grid.rows - Number of rows in the grid.children - Components to add to the grid.AbstractComponentContainer.addComponents(Component...)protected GridLayoutState getState()
AbstractComponentgetState in class AbstractLayoutprotected GridLayoutState getState(boolean markAsDirty)
AbstractClientConnectorgetState in class AbstractComponentmarkAsDirty - true if the connector should automatically be marked dirty,
false otherwiseAbstractClientConnector.getState()public void addComponent(Component component, int column1, int row1, int column2, int row2) throws GridLayout.OverlapsException, GridLayout.OutOfBoundsException
Adds a component to the grid in the specified area. The area is defined by specifying the upper left corner (column1, row1) and the lower right corner (column2, row2) of the area. The coordinates are zero-based.
If the area overlaps with any of the existing components already present
in the grid, the operation will fail and an GridLayout.OverlapsException is
thrown.
component - the component to be added, not null.column1 - the column of the upper left corner of the area c
is supposed to occupy. The leftmost column has index 0.row1 - the row of the upper left corner of the area c is
supposed to occupy. The topmost row has index 0.column2 - the column of the lower right corner of the area
c is supposed to occupy.row2 - the row of the lower right corner of the area c
is supposed to occupy.GridLayout.OverlapsException - if the new component overlaps with any of the components
already in the grid.GridLayout.OutOfBoundsException - if the cells are outside the grid area.public void addComponent(Component component, int column, int row) throws GridLayout.OverlapsException, GridLayout.OutOfBoundsException
component - the component to be added, not null.column - the column index, starting from 0.row - the row index, starting from 0.GridLayout.OverlapsException - if the new component overlaps with any of the components
already in the grid.GridLayout.OutOfBoundsException - if the cell is outside the grid area.public void newLine()
Sets the cursor column to 0 and increments the cursor row by one.
By calling this function you can ensure that no more components are added right of the previous component.
space()public void space()
newLine()public void addComponent(Component component)
addComponent in interface ComponentContaineraddComponent in class AbstractComponentContainercomponent - the component to be added, not null.ComponentContainer.addComponent(Component)public void removeComponent(Component component)
removeComponent in interface ComponentContainerremoveComponent in class AbstractComponentContainercomponent - the component to be removed.ComponentContainer.removeComponent(Component)public void removeComponent(int column,
int row)
column - the component's column, starting from 0.row - the component's row, starting from 0.public Iterator<Component> iterator()
iterator in interface HasComponentsiterator in interface Iterable<Component>public int getComponentCount()
AbstractComponentContainer.getComponentIterator().getComponentCount in interface ComponentContainerpublic void beforeClientResponse(boolean initial)
ClientConnectorbeforeClientResponse in interface ClientConnectorbeforeClientResponse in class AbstractComponentinitial - true if the client-side connector will be created
and initialized after this method has been invoked.
false if there is already an initialized
client-side connector.public Alignment getComponentAlignment(Component childComponent)
Layout.AlignmentHandlergetComponentAlignment in interface Layout.AlignmentHandlerAlignmentpublic void setColumns(int columns)
columns - the new number of columns in the grid.public int getColumns()
public void setRows(int rows)
rows - the new number of rows in the grid.public int getRows()
public int getCursorX()
The cursor position points the position for the next component that is added without specifying its coordinates (grid cell). When the cursor position is occupied, the next component will be added to first free position after the cursor.
public void setCursorX(int cursorX)
cursorX - public int getCursorY()
The cursor position points the position for the next component that is added without specifying its coordinates (grid cell). When the cursor position is occupied, the next component will be added to the first free position after the cursor.
public void setCursorY(int cursorY)
cursorY - the row number, starting from 0 for the topmost row.public void replaceComponent(Component oldComponent, Component newComponent)
ComponentContainerThis method replaces component with another one is such way that the new component overtakes the position of the old component. If the old component is not in the container, the new component is added to the container. If the both component are already in the container, their positions are swapped. Component attach and detach events should be taken care as with add and remove.
replaceComponent in interface ComponentContaineroldComponent - the old component that will be replaced.newComponent - the new component to be replaced.public void removeAllComponents()
AbstractComponentContainerremoveAllComponents in interface ComponentContainerremoveAllComponents in class AbstractComponentContainerpublic void setComponentAlignment(Component childComponent, Alignment alignment)
Layout.AlignmentHandler
layout.setComponentAlignment(myComponent, Alignment.TOP_RIGHT);
setComponentAlignment in interface Layout.AlignmentHandlerchildComponent - the component to align within it's layout cell.alignment - the Alignment value to be setpublic void setSpacing(boolean spacing)
Layout.SpacingHandler
NOTE: This will only affect the space between
components, not the space around all the components in the layout
(i.e. do not confuse this with the cellspacing attribute of a HTML
Table). Use #setMargin(boolean) to add space around the
layout.
See the reference manual for more information about CSS rules for defining the amount of spacing to use.
setSpacing in interface Layout.SpacingHandlerspacing - true if spacing should be turned on, false if it should be
turned offpublic boolean isSpacing()
isSpacing in interface Layout.SpacingHandlerpublic void insertRow(int row)
row - Index of the row before which the new row will be inserted.
The leftmost row has index 0.public void removeRow(int row)
Components which span over several rows are removed if the selected row is on the first row of such a component.
If the last row is removed then all remaining components will be removed and the grid will be reduced to one row. The cursor will be moved to the upper left cell of the grid.
row - Index of the row to remove. The leftmost row has index 0.public void setColumnExpandRatio(int columnIndex,
float ratio)
The expand ratio defines how excess space is distributed among columns. Excess space means space that is left over from components that are not sized relatively. By default, the excess space is distributed evenly.
Note, that width of this GridLayout needs to be defined (fixed or relative, as opposed to undefined height) for this method to have any effect.
Note that checking for relative width for the child components is done on
the server so you cannot set a child component to have undefined width on
the server and set it to 100% in CSS. You must set it to
100% on the server.
columnIndex - ratio - #setWidth(float, int)public float getColumnExpandRatio(int columnIndex)
columnIndex - setColumnExpandRatio(int, float)public void setRowExpandRatio(int rowIndex,
float ratio)
Expand ratio defines how excess space is distributed among rows. Excess space means the space left over from components that are not sized relatively. By default, the excess space is distributed evenly.
Note, that height of this GridLayout needs to be defined (fixed or relative, as opposed to undefined height) for this method to have any effect.
Note that checking for relative height for the child components is done
on the server so you cannot set a child component to have undefined
height on the server and set it to 100% in CSS. You must set
it to 100% on the server.
rowIndex - The row index, starting from 0 for the topmost row.ratio - #setHeight(float, int)public float getRowExpandRatio(int rowIndex)
rowIndex - The row index, starting from 0 for the topmost row.setRowExpandRatio(int, float)public Component getComponent(int x, int y)
x - The column index, starting from 0 for the leftmost column.y - The row index, starting from 0 for the topmost row.public GridLayout.Area getComponentArea(Component component)
component - the component whose area information is requested.public void addLayoutClickListener(LayoutEvents.LayoutClickListener listener)
LayoutEvents.LayoutClickNotifierLayoutEvents.LayoutClickEvent.
Use #removeListener(LayoutClickListener) to remove the
listener.addLayoutClickListener in interface LayoutEvents.LayoutClickNotifierlistener - The listener to add@Deprecated public void addListener(LayoutEvents.LayoutClickListener listener)
#addLayoutClickListener(LayoutClickListener)addListener in interface LayoutEvents.LayoutClickNotifierpublic void removeLayoutClickListener(LayoutEvents.LayoutClickListener listener)
LayoutEvents.LayoutClickNotifierremoveLayoutClickListener in interface LayoutEvents.LayoutClickNotifierlistener - LayoutClickListener to be removed@Deprecated public void removeListener(LayoutEvents.LayoutClickListener listener)
#removeLayoutClickListener(LayoutClickListener)removeListener in interface LayoutEvents.LayoutClickNotifierpublic void setMargin(boolean enabled)
Layout.MarginHandlersetMargin in interface Layout.MarginHandlerenabled - true if margins should be enabled on all sides, false to
disable all marginspublic void setMargin(MarginInfo marginInfo)
Layout.MarginHandler
NOTE: This will only affect the space around the
components in the layout, not space between the components in the
layout. Use #setSpacing(boolean) to add space between the
components in the layout.
See the reference manual for more information about CSS rules for defining the size of the margin.
setMargin in interface Layout.MarginHandlermarginInfo - MarginInfo object containing the new margins.public MarginInfo getMargin()
getMargin in interface Layout.MarginHandlerpublic Alignment getDefaultComponentAlignment()
Layout.AlignmentHandlergetDefaultComponentAlignment in interface Layout.AlignmentHandlerpublic void setDefaultComponentAlignment(Alignment defaultAlignment)
Layout.AlignmentHandlerAlignment.TOP_LEFT.setDefaultComponentAlignment in interface Layout.AlignmentHandlerdefaultAlignment - The new default alignmentpublic void setHideEmptyRowsAndColumns(boolean hideEmptyRowsAndColumns)
hideEmptyRowsAndColumns - true to hide empty rows and columns, false to leave them as-ispublic boolean isHideEmptyRowsAndColumns()
setHideEmptyRowsAndColumns(boolean)public void readDesign(org.jsoup.nodes.Element design,
DesignContext designContext)
The component is responsible not only for updating its own state but also for ensuring that its children update their state based on the design.
It is assumed that the component is in its default state when this method is called. Reading should only take into consideration attributes specified in the design and not reset any unspecified attributes to their defaults.
This method must not modify the design.
After reading the design, cursorY is set to point to a row outside of the GridLayout area. CursorX is reset to 0.
readDesign in interface ComponentreadDesign in class AbstractComponentdesign - The element to obtain the state fromdesignContext - The DesignContext instance used for parsing the designpublic void writeDesign(org.jsoup.nodes.Element design,
DesignContext designContext)
ComponentThe component is responsible not only for writing its own state but also for ensuring that its children write their state to the design.
This method must not modify the component state.
writeDesign in interface ComponentwriteDesign in class AbstractComponentdesign - The element to write the component state to. Any previous
attributes or child nodes are not cleared.designContext - The DesignContext instance used for writing the designprotected Collection<String> getCustomAttributes()
AbstractComponentreadDesign and writeDesign
methods. Typically these are handled in a custom way in the overridden
versions of the above methodsgetCustomAttributes in class AbstractComponentCopyright © 2022 Vaadin Ltd. All rights reserved.