LayoutManager
public class GridLayout extends Object implements LayoutManager
You use the GridLayout
by specifying a number of columns you want your grid to have and then when you add
components, you assign LayoutData
to these components using the different static methods in this class
(createLayoutData(..)
). You can set components to span both rows and columns, as well as defining how to
distribute the available space.
Modifier and Type | Class | Description |
---|---|---|
static class |
GridLayout.Alignment |
The enum is used to specify where in a grid cell a component should be placed, in the case that the preferred
size of the component is smaller than the space in the cell.
|
Constructor | Description |
---|---|
GridLayout(int numberOfColumns) |
Creates a new
GridLayout with the specified number of columns. |
Modifier and Type | Method | Description |
---|---|---|
static LayoutData |
createHorizontallyEndAlignedLayoutData(int horizontalSpan) |
This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can
vertically and make the component occupy the whole area vertically and center it horizontally
|
static LayoutData |
createHorizontallyFilledLayoutData() |
This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can
horizontally and make the component occupy the whole area horizontally and center it vertically, spanning 1 cell.
|
static LayoutData |
createHorizontallyFilledLayoutData(int horizontalSpan) |
This is a shortcut method that will create a grid layout data object that will expand its cell as much as is can
horizontally and make the component occupy the whole area horizontally and center it vertically
|
static LayoutData |
createLayoutData(GridLayout.Alignment horizontalAlignment,
GridLayout.Alignment verticalAlignment) |
Creates a layout data object for
GridLayout :s that specify the horizontal and vertical alignment for the
component in case the cell space is larger than the preferred size of the component |
static LayoutData |
createLayoutData(GridLayout.Alignment horizontalAlignment,
GridLayout.Alignment verticalAlignment,
boolean grabExtraHorizontalSpace,
boolean grabExtraVerticalSpace) |
Creates a layout data object for
GridLayout :s that specify the horizontal and vertical alignment for the
component in case the cell space is larger than the preferred size of the component. |
static LayoutData |
createLayoutData(GridLayout.Alignment horizontalAlignment,
GridLayout.Alignment verticalAlignment,
boolean grabExtraHorizontalSpace,
boolean grabExtraVerticalSpace,
int horizontalSpan,
int verticalSpan) |
Creates a layout data object for
GridLayout :s that specify the horizontal and vertical alignment for the
component in case the cell space is larger than the preferred size of the component. |
void |
doLayout(TerminalSize area,
List<Component> components) |
Given a size constraint, update the location and size of each component in the component list by laying them out
in the available area.
|
int |
getBottomMarginSize() |
Returns the bottom margin, i.e. number of empty rows below the last row in the grid
|
int |
getHorizontalSpacing() |
Returns the horizontal spacing, i.e. the number of empty columns between each cell
|
int |
getLeftMarginSize() |
Returns the left margin, i.e. number of empty columns left of the first column in the grid
|
TerminalSize |
getPreferredSize(List<Component> components) |
This method returns the dimensions it would prefer to have to be able to layout all components while giving all
of them as much space as they are asking for.
|
int |
getRightMarginSize() |
Returns the right margin, i.e. number of empty columns right of the last column in the grid
|
int |
getTopMarginSize() |
Returns the top margin, i.e. number of empty rows above the first row in the grid
|
int |
getVerticalSpacing() |
Returns the vertical spacing, i.e. the number of empty columns between each row
|
boolean |
hasChanged() |
Returns true if the internal state of this LayoutManager has changed since the last call to doLayout.
|
GridLayout |
setBottomMarginSize(int bottomMarginSize) |
Sets the bottom margin, i.e. number of empty rows below the last row in the grid
|
GridLayout |
setHorizontalSpacing(int horizontalSpacing) |
Sets the horizontal spacing, i.e. the number of empty columns between each cell
|
GridLayout |
setLeftMarginSize(int leftMarginSize) |
Sets the left margin, i.e. number of empty columns left of the first column in the grid
|
GridLayout |
setRightMarginSize(int rightMarginSize) |
Sets the right margin, i.e. number of empty columns right of the last column in the grid
|
GridLayout |
setTopMarginSize(int topMarginSize) |
Sets the top margin, i.e. number of empty rows above the first row in the grid
|
GridLayout |
setVerticalSpacing(int verticalSpacing) |
Sets the vertical spacing, i.e. the number of empty columns between each row
|
public GridLayout(int numberOfColumns)
GridLayout
with the specified number of columns. Initially, this layout will have a
horizontal spacing of 1 and vertical spacing of 0, with a left and right margin of 1.numberOfColumns
- Number of columns in this gridpublic static LayoutData createLayoutData(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment)
GridLayout
:s that specify the horizontal and vertical alignment for the
component in case the cell space is larger than the preferred size of the componenthorizontalAlignment
- Horizontal alignment strategyverticalAlignment
- Vertical alignment strategypublic static LayoutData createLayoutData(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment, boolean grabExtraHorizontalSpace, boolean grabExtraVerticalSpace)
GridLayout
:s that specify the horizontal and vertical alignment for the
component in case the cell space is larger than the preferred size of the component. This method also has fields
for indicating that the component would like to take more space if available to the container. For example, if
the container is assigned is assigned an area of 50x15, but all the child components in the grid together only
asks for 40x10, the remaining 10 columns and 5 rows will be empty. If just a single component asks for extra
space horizontally and/or vertically, the grid will expand out to fill the entire area and the text space will be
assigned to the component that asked for it.horizontalAlignment
- Horizontal alignment strategyverticalAlignment
- Vertical alignment strategygrabExtraHorizontalSpace
- If set to true
, this component will ask to be assigned extra horizontal
space if there is any to assigngrabExtraVerticalSpace
- If set to true
, this component will ask to be assigned extra vertical
space if there is any to assignpublic static LayoutData createLayoutData(GridLayout.Alignment horizontalAlignment, GridLayout.Alignment verticalAlignment, boolean grabExtraHorizontalSpace, boolean grabExtraVerticalSpace, int horizontalSpan, int verticalSpan)
GridLayout
:s that specify the horizontal and vertical alignment for the
component in case the cell space is larger than the preferred size of the component. This method also has fields
for indicating that the component would like to take more space if available to the container. For example, if
the container is assigned is assigned an area of 50x15, but all the child components in the grid together only
asks for 40x10, the remaining 10 columns and 5 rows will be empty. If just a single component asks for extra
space horizontally and/or vertically, the grid will expand out to fill the entire area and the text space will be
assigned to the component that asked for it. It also puts in data on how many rows and/or columns the component
should span.horizontalAlignment
- Horizontal alignment strategyverticalAlignment
- Vertical alignment strategygrabExtraHorizontalSpace
- If set to true
, this component will ask to be assigned extra horizontal
space if there is any to assigngrabExtraVerticalSpace
- If set to true
, this component will ask to be assigned extra vertical
space if there is any to assignhorizontalSpan
- How many "cells" this component wants to span horizontallyverticalSpan
- How many "cells" this component wants to span verticallypublic static LayoutData createHorizontallyFilledLayoutData()
public static LayoutData createHorizontallyFilledLayoutData(int horizontalSpan)
horizontalSpan
- How many cells to span horizontallypublic static LayoutData createHorizontallyEndAlignedLayoutData(int horizontalSpan)
horizontalSpan
- How many cells to span verticallypublic int getHorizontalSpacing()
public GridLayout setHorizontalSpacing(int horizontalSpacing)
horizontalSpacing
- New horizontal spacingpublic int getVerticalSpacing()
public GridLayout setVerticalSpacing(int verticalSpacing)
verticalSpacing
- New vertical spacingpublic int getTopMarginSize()
public GridLayout setTopMarginSize(int topMarginSize)
topMarginSize
- Top margin, in number of rowspublic int getBottomMarginSize()
public GridLayout setBottomMarginSize(int bottomMarginSize)
bottomMarginSize
- Bottom margin, in number of rowspublic int getLeftMarginSize()
public GridLayout setLeftMarginSize(int leftMarginSize)
leftMarginSize
- Left margin, in number of columnspublic int getRightMarginSize()
public GridLayout setRightMarginSize(int rightMarginSize)
rightMarginSize
- Right margin, in number of columnspublic boolean hasChanged()
LayoutManager
hasChanged
in interface LayoutManager
true
if this layout manager's internal state has changed since the last call to doLayout
public TerminalSize getPreferredSize(List<Component> components)
LayoutManager
getPreferredSize
in interface LayoutManager
components
- List of componentspublic void doLayout(TerminalSize area, List<Component> components)
LayoutManager
setPosition(..)
and setSize(..)
on the Components.doLayout
in interface LayoutManager
area
- Size available to this layout manager to lay out the components oncomponents
- List of components to lay outCopyright © 2020. All rights reserved.