public class BorderLayout
A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER. When adding a component to a container with a border layout, use one of these five constants.
The border layout scales all of the components within it to match the available constraints. The NORTH & SOUTH components use their preferred height but are stretched to take up the full width available. The EAST & WEST do the same for the reverse axis however they leave room for the NORTH/SOUTH entries if they are defined.
The CENTER constraint will take up the rest of the available space regardless of its preferred
size. This is normally very useful, however in some cases we would prefer that the center
component will actually position itself in the middle of the available space. For this we have
the setCenterBehavior method.
Because of its scaling behavior scrolling a border layout makes no sense. However it is a
common mistake to apply a border layout to a scrollable container or trying to make a border
layout scrollable. That is why the com.codename1.ui.Container class explicitly blocks
scrolling on a BorderLayout.
Typical usage of this class:
Form hi = new Form("Border Layout", new BorderLayout());
hi.add(BorderLayout.CENTER, new Label("Center")).
add(BorderLayout.SOUTH, new Label("South")).
add(BorderLayout.NORTH, new Label("North")).
add(BorderLayout.EAST, new Label("East")).
add(BorderLayout.WEST, new Label("West"));
hi.show();
When defining the center behavior we can get very different results:
Form hi = new Form("Border Layout", new BorderLayout());
((BorderLayout)hi.getLayout()).setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER);
hi.add(BorderLayout.CENTER, new Label("Center")).
add(BorderLayout.SOUTH, new Label("South")).
add(BorderLayout.NORTH, new Label("North")).
add(BorderLayout.EAST, new Label("East")).
add(BorderLayout.WEST, new Label("West"));
hi.show();
Notice that in the case of RTL (right to left language also known as bidi) the EAST and WEST values are implicitly reversed as shown in this image:
You can read further in the BorderLayout section in the developer guide.
Fields
public static final int CENTER_BEHAVIOR_SCALE = 0 | Defines the behavior of the component placed in the center position of the layout, by default it is scaled to the available space |
public static final int CENTER_BEHAVIOR_CENTER = 1 | Defines the behavior of the component placed in the center position of the layout, places the component in the center of the space available to the center component. |
public static final int CENTER_BEHAVIOR_CENTER_ABSOLUTE = 2 | Defines the behavior of the component placed in the center position of the layout, places the component in the center of the surrounding container |
public static final int CENTER_BEHAVIOR_TOTAL_BELLOW = 3 | Deprecated Deprecated due to spelling mistake, use CENTER_BEHAVIOR_TOTAL_BELOW The center component takes up the entire screens and the sides are automatically placed on top of it thus creating a layered effect |
public static final int CENTER_BEHAVIOR_TOTAL_BELOW = 3 | The center component takes up the entire screens and the sides are automatically placed on top (or below based on z-order) thus creating a layered effect |
public static final String NORTH = "North" | The north layout constraint (top of container). |
public static final String SOUTH = "South" | The south layout constraint (bottom of container). |
public static final String CENTER = "Center" | The center layout constraint (middle of container) |
public static final String WEST = "West" | The west layout constraint (left of container). |
public static final String EAST = "East" | The east layout constraint (right of container). |
public static final String OVERLAY = "Overlay" | Overlay on top of the other layout components |
Constructors
public BorderLayout() | Creates a new instance of BorderLayout |
public BorderLayout(int behavior) | Creates a new instance of BorderLayout with absolute behavior |
Methods
public static BorderLayout center() | Shorthand for new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER) |
public static BorderLayout absolute() | Shorthand for new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE) |
public static BorderLayout totalBelow() | Shorthand for new BorderLayout(BorderLayout.CENTER_BEHAVIOR_TOTAL_BELOW) |
public static Container center(Component center) | Convenience method that creates a border layout container and places the given component in the center |
public static Container centerEastWest(Component center, Component east, Component west) | Convenience method that creates a border layout container and places the given component in the center east and west respectively |
public static Container centerAbsoluteEastWest(Component center, Component east, Component west) | Convenience method that creates a border layout absolute center container and places the given component in the center east and west respectively |
public static Container centerCenterEastWest(Component center, Component east, Component west) | Convenience method that creates a border layout center container and places the given component in the center east and west respectively with the #CENTER_BEHAVIOR_CENTER constraint applied |
public static Container centerTotalBelowEastWest(Component center, Component east, Component west) | Convenience method that creates a border layout center container and places the given component in the center east and west respectively with the #CENTER_BEHAVIOR_TOTAL_BELOW constraint applied |
public static Container centerCenter(Component center) | Convenience method that creates a border layout container and places the given component in the center with the #CENTER_BEHAVIOR_CENTER constraint applied |
public static Container centerAbsolute(Component center) | Convenience method that creates a border layout container and places the given component in the center with the #CENTER_BEHAVIOR_CENTER_ABSOLUTE constraint applied |
public static Container centerTotalBelow(Component center) | Convenience method that creates a border layout container and places the given component in the center with the #CENTER_BEHAVIOR_TOTAL_BELOW constraint applied |
public static Container north(Component north) | Convenience method that creates a border layout container and places the given component in the north |
public static Container south(Component south) | Convenience method that creates a border layout container and places the given component in the south |
public static Container east(Component east) | Convenience method that creates a border layout container and places the given component in the east |
public static Container west(Component west) | Convenience method that creates a border layout container and places the given component in the west |
public void addLayoutComponent(Object name, Component comp, Container c) | Some layouts can optionally track the addition of elements with meta-data that allows the user to “hint” on object positioning. |
public void removeLayoutComponent(Component comp) | Removes the component from the layout this operation is only useful if the layout maintains references to components within it |
public Object getComponentConstraint(Component comp) | Returns the component constraint |
public void layoutContainer(Container target) | Layout the given parent container children |
public Dimension getPreferredSize(Container parent) | Returns the container preferred size |
public Component getSouth() | Returns the component in the south location |
public Component getCenter() | Returns the component in the center location |
public Component getNorth() | Returns the component in the north location |
public Component getEast() | Returns the component in the east location |
public Component getWest() | Returns the component in the west location |
public Component getOverlay() | Returns overlay component. |
public String toString() | Returns a string representation of the object. |
public void defineLandscapeSwap(String portraitPosition, String landscapePosition) | This method allows swapping positions within the border layout when the layout orientation changes to landscape or if the layout starts off as landscape. |
public String getLandscapeSwap(String portraitPosition) | Returns the landscape swap destination for the given border layout element if such a destination is defined. |
public boolean equals(Object o) | Indicates whether some other object is “equal to” this one. |
public int hashCode() | Returns a hash code value for the object. |
public boolean isAbsoluteCenter() | Deprecated Indicates that the center shouldn’t grow and should be placed exactly in the center of the layout |
public void setAbsoluteCenter(boolean absoluteCenter) | Deprecated Indicates that the center shouldn’t grow and should be placed exactly in the center of the layout |
public int getCenterBehavior() | Defines the behavior of the center component to one of the constants defined in this class |
public void setCenterBehavior(int centerBehavior) | Defines the behavior of the center component to one of the constants defined in this class |
public boolean isOverlapSupported() | This method returns true if the Layout allows Components to Overlap. |
public boolean isScaleEdges() | Stretches the edge components (NORTH/EAST/WEST/SOUTH) |
public void setScaleEdges(boolean scaleEdges) | Stretches the edge components (NORTH/EAST/WEST/SOUTH) |
public boolean isConstraintTracking() | If this method returns true, the addLayoutComponent method will be called when replacing a layout for every component within the container |
public boolean obscuresPotential(Container parent) | Some layout managers can obscure their child components in some cases this returns true if the basic underpinnings are in place for that. |
public boolean overridesTabIndices(Container parent) | If a layout specifies a different traversal order of its components than the component index, then it should override this method to return true, and it should also override #getChildrenInTraversalOrder(com.codename1.ui.Container) to set the tab indices of a container’s children. |
protected Component[] getChildrenInTraversalOrder(Container parent) | Gets the children of the parent container in the order that they should be traversed when tabbing through a form. |
Inherited methods
Field details
CENTER_BEHAVIOR_SCALE
public static final int CENTER_BEHAVIOR_SCALE = 0CENTER_BEHAVIOR_CENTER
public static final int CENTER_BEHAVIOR_CENTER = 1CENTER_BEHAVIOR_CENTER_ABSOLUTE
public static final int CENTER_BEHAVIOR_CENTER_ABSOLUTE = 2CENTER_BEHAVIOR_TOTAL_BELLOW
public static final int CENTER_BEHAVIOR_TOTAL_BELLOW = 3CENTER_BEHAVIOR_TOTAL_BELOW
public static final int CENTER_BEHAVIOR_TOTAL_BELOW = 3NORTH
public static final String NORTH = "North"SOUTH
public static final String SOUTH = "South"CENTER
public static final String CENTER = "Center"WEST
public static final String WEST = "West"EAST
public static final String EAST = "East"OVERLAY
public static final String OVERLAY = "Overlay"Constructor details
BorderLayout
public BorderLayout()BorderLayout
public BorderLayout(int behavior)Parameters
behaviorint- identical value as the setCenterBehavior method
Method details
center
public static BorderLayout center()new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER)Returns
BorderLayout with #CENTER_BEHAVIOR_CENTER constraint appliedabsolute
public static BorderLayout absolute()new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE)Returns
BorderLayout with #CENTER_BEHAVIOR_CENTER_ABSOLUTE constraint appliedtotalBelow
public static BorderLayout totalBelow()new BorderLayout(BorderLayout.CENTER_BEHAVIOR_TOTAL_BELOW)Returns
BorderLayout with #CENTER_BEHAVIOR_TOTAL_BELOW constraint appliedcenter
public static Container center(Component center)Parameters
centerComponent- the center component
Returns
centerEastWest
public static Container centerEastWest(Component center, Component east, Component west)Parameters
centerComponent- the center component
eastComponent- component or null to ignore
westComponent- component or null to ignore
Returns
centerAbsoluteEastWest
public static Container centerAbsoluteEastWest(Component center, Component east, Component west)Parameters
centerComponent- the center component
eastComponent- component or null to ignore
westComponent- component or null to ignore
Returns
centerCenterEastWest
public static Container centerCenterEastWest(Component center, Component east, Component west)#CENTER_BEHAVIOR_CENTER constraint appliedParameters
centerComponent- the center component
eastComponent- component or null to ignore
westComponent- component or null to ignore
Returns
centerTotalBelowEastWest
public static Container centerTotalBelowEastWest(Component center, Component east, Component west)#CENTER_BEHAVIOR_TOTAL_BELOW constraint appliedParameters
centerComponent- the center component
eastComponent- component or null to ignore
westComponent- component or null to ignore
Returns
centerCenter
public static Container centerCenter(Component center)#CENTER_BEHAVIOR_CENTER constraint appliedParameters
centerComponent- the center component
Returns
centerAbsolute
public static Container centerAbsolute(Component center)#CENTER_BEHAVIOR_CENTER_ABSOLUTE constraint appliedParameters
centerComponent- the center component
Returns
centerTotalBelow
public static Container centerTotalBelow(Component center)#CENTER_BEHAVIOR_TOTAL_BELOW constraint appliedParameters
centerComponent- the center component
Returns
north
public static Container north(Component north)Parameters
northComponent- the north component
Returns
south
public static Container south(Component south)Parameters
southComponent- the south component
Returns
east
public static Container east(Component east)Parameters
eastComponent- the east component
Returns
west
public static Container west(Component west)Parameters
westComponent- the west component
Returns
addLayoutComponent
public void addLayoutComponent(Object name, Component comp, Container c)Parameters
nameObject- optional meta data information, like alignment orientation
compComponent- the added component to the layout
cContainer- the parent container
removeLayoutComponent
public void removeLayoutComponent(Component comp)Parameters
compComponent- the removed component from layout
getComponentConstraint
public Object getComponentConstraint(Component comp)Parameters
compComponent- the component whose constraint is queried
Returns
layoutContainer
public void layoutContainer(Container target)Parameters
targetContainer- the given parent container
getPreferredSize
public Dimension getPreferredSize(Container parent)Parameters
parentContainer- the parent container
Returns
getSouth
public Component getSouth()Returns
getCenter
public Component getCenter()Returns
getNorth
public Component getNorth()Returns
getEast
public Component getEast()Returns
getWest
public Component getWest()Returns
getOverlay
public Component getOverlay()Returns
toString
public String toString()defineLandscapeSwap
public void defineLandscapeSwap(String portraitPosition, String landscapePosition)Parameters
portraitPositionString- the position for the component when in portrait (this position should always be used when adding a component to the layout). One of NORTH/SOUTH/EAST/WEST/CENTER.
landscapePositionString- the destination position to use in landscape
getLandscapeSwap
public String getLandscapeSwap(String portraitPosition)Parameters
portraitPositionString- the constraint used when placing the component
Returns
equals
public boolean equals(Object o)hashCode
public int hashCode()isAbsoluteCenter
public boolean isAbsoluteCenter()Returns
setAbsoluteCenter
public void setAbsoluteCenter(boolean absoluteCenter)Parameters
absoluteCenterboolean- the absoluteCenter to set
getCenterBehavior
public int getCenterBehavior()Returns
setCenterBehavior
public void setCenterBehavior(int centerBehavior)Parameters
centerBehaviorint- the centerBehavior to set
isOverlapSupported
public boolean isOverlapSupported()Returns
isScaleEdges
public boolean isScaleEdges()Returns
setScaleEdges
public void setScaleEdges(boolean scaleEdges)Parameters
scaleEdgesboolean- the scaleEdges to set
isConstraintTracking
public boolean isConstraintTracking()Returns
obscuresPotential
public boolean obscuresPotential(Container parent)Parameters
parentContainer- parent container
Returns
overridesTabIndices
public boolean overridesTabIndices(Container parent)#getChildrenInTraversalOrder(com.codename1.ui.Container)
to set the tab indices of a container’s children.Parameters
parentContainer- The parent component.
Returns
getChildrenInTraversalOrder
protected Component[] getChildrenInTraversalOrder(Container parent)Gets the children of the parent container in the order that they should be traversed when tabbing through a form.
This should only be overridden if the Layout defines a different traversal order than the standard index order.
Layouts that implement this method, should override the #overridesTabIndices(com.codename1.ui.Container)
method to return true.