Groovy Documentation

com.wordpress.tipsforjava.swing
[Java] Class RelativeLayout

java.lang.Object
  com.wordpress.tipsforjava.swing.RelativeLayout
All Implemented Interfaces:
LayoutManager2, Serializable

public class RelativeLayout
extends Object

The RelativeLayout class is a layout manager that lays out a container's components on the specified X or Y axis. Components can be layed out at their preferred size or at a relative size. When relative sizing is used the component must be added to the container using a relative size constraint, which is simply a Float value. The space available for relative sized components is determined by subtracting the preferred size of the other components from the space available in the container. Each component is then assigned a size based on its relative size value. For example: container.add(component1, new Float(1)); container.add(component2, new Float(2)); There is a total of 3 relative units. If the container has 300 pixels of space available then component1 will get 100 and component2, 200. It is possible that rounding errors will occur in which case you can specify a rounding policy to use to allocate the extra pixels. By defaults components are center aligned on the secondary axis however this can be changed at the container or component level.

Authors:
Rob Camick
Darryl Burke


Field Summary
static float CENTER

static float COMPONENT

static int DO_NOTHING

static int EQUAL

static int FIRST

static int LARGEST

static int LAST

static float LEADING

private static int MINIMUM

private static int PREFERRED

static float TRAILING

static int X_AXIS

static int Y_AXIS

private float alignment

The alignment of the Components on the other axis of the Container.

private int axis

The axis of the Components within the Container.

private int borderGap

The gap (in pixels) used before the leading component and after the trailing component.

private HashMap constraints

private boolean fill

private int fillGap

private int gap

This is the gap (in pixels) which specifies the space between components It can be changed at any time and should be a non-negative integer.

private int roundingPolicy

 
Constructor Summary
RelativeLayout()

Creates a relative layout with the components layed out on the X-Axis using the default gap

RelativeLayout(int axis)

Creates a relative layout with the components layed out on the specified axis using the default gap

RelativeLayout(int axis, int gap)

Creates a relative layout with the components layed out on the specified axis using the specfied gap

 
Method Summary
void addLayoutComponent(String name, Component component)

Not supported

void addLayoutComponent(Component component, Object constraint)

private void adjustEqual(int[] relativeSpace, int spaceRemaining)

private void adjustFirst(int[] relativeSpace, int spaceRemaining)

protected void adjustForRounding(int[] relativeSpace, int spaceRemaining)

private void adjustLargest(int[] relativeSpace, int spaceRemaining)

private void adjustLast(int[] relativeSpace, int spaceRemaining)

private int[] allocateRelativeSpace(Container parent, int spaceAvailable, float relativeTotal)

float getAlignment()

Gets the alignment of the components on the opposite axis.

int getAxis()

Gets the layout axis.

int getBorderGap()

Gets the initial gap.

Float getConstraints(Component component)

Gets the constraints for the specified component.

private Dimension getDimension(Component component, int type)

int getFillGap()

Gets the fill gap amount.

int getGap()

Gets the gap between components.

float getLayoutAlignmentX(Container parent)

Returns the alignment along the x axis.

float getLayoutAlignmentY(Container parent)

Returns the alignment along the y axis.

private Dimension getLayoutSize(Container parent, int type)

private int getLocationX(Component component, int width)

private int getLocationY(Component component, int height)

int getRoundingPolicy()

Gets the rounding policy.

private int getVisibleComponents(Container container)

void invalidateLayout(Container target)

Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.

boolean isFill()

Gets the fill property for the component size on the opposite edge.

void layoutContainer(Container parent)

Lays out the specified container using this layout.

private void layoutContainerHorizontally(Container parent)

private void layoutContainerVertically(Container parent)

Dimension maximumLayoutSize(Container target)

There is no maximum.

Dimension minimumLayoutSize(Container parent)

Determines the minimum size of the container argument using this column layout.

Dimension preferredLayoutSize(Container parent)

Determines the preferred size of the container argument using this column layout.

void removeLayoutComponent(Component comp)

Removes the specified component from the layout.

void setAlignment(float alignment)

void setAxis(int axis)

Sets the layout axis

void setBorderGap(int borderGap)

Sets the initial gap.

void setFill(boolean fill)

Change size of relative components to fill the space available For X-AXIS aligned components the height will be filled.

void setFillGap(int fillGap)

Specify the number of pixels by which the fill size is decreased when setFill(true) has been specified.

void setGap(int gap)

Sets the gap between components to the specified value.

void setRoundingPolicy(int roundingPolicy)

Specify the rounding policy to be used when all the avialable pixels have not been allocated to a component.

String toString()

Returns the string representation of this column layout's values.

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

CENTER

public static final float CENTER


COMPONENT

public static final float COMPONENT


DO_NOTHING

public static final int DO_NOTHING


EQUAL

public static final int EQUAL


FIRST

public static final int FIRST


LARGEST

public static final int LARGEST


LAST

public static final int LAST


LEADING

public static final float LEADING


MINIMUM

private static final int MINIMUM


PREFERRED

private static final int PREFERRED


TRAILING

public static final float TRAILING


X_AXIS

public static final int X_AXIS


Y_AXIS

public static final int Y_AXIS


alignment

private float alignment
The alignment of the Components on the other axis of the Container. For X-AXIS this would refer to the Y alignemt. For Y-AXIS this would refer to the X alignment.


axis

private int axis
The axis of the Components within the Container.


borderGap

private int borderGap
The gap (in pixels) used before the leading component and after the trailing component. It can be changed at any time and should be a non-negative integer.


constraints

private HashMap constraints


fill

private boolean fill


fillGap

private int fillGap


gap

private int gap
This is the gap (in pixels) which specifies the space between components It can be changed at any time and should be a non-negative integer.


roundingPolicy

private int roundingPolicy


 
Constructor Detail

RelativeLayout

public RelativeLayout()
Creates a relative layout with the components layed out on the X-Axis using the default gap


RelativeLayout

public RelativeLayout(int axis)
Creates a relative layout with the components layed out on the specified axis using the default gap

Parameters:
axis - X-AXIS or Y_AXIS


RelativeLayout

public RelativeLayout(int axis, int gap)
Creates a relative layout with the components layed out on the specified axis using the specfied gap

All RelativeLayout constructors defer to this one.

Parameters:
axis - X-AXIS or Y_AXIS
gap - the gap


 
Method Detail

addLayoutComponent

public void addLayoutComponent(String name, Component component)
Not supported


addLayoutComponent

public void addLayoutComponent(Component component, Object constraint)


adjustEqual

private void adjustEqual(int[] relativeSpace, int spaceRemaining)


adjustFirst

private void adjustFirst(int[] relativeSpace, int spaceRemaining)


adjustForRounding

protected void adjustForRounding(int[] relativeSpace, int spaceRemaining)


adjustLargest

private void adjustLargest(int[] relativeSpace, int spaceRemaining)


adjustLast

private void adjustLast(int[] relativeSpace, int spaceRemaining)


allocateRelativeSpace

private int[] allocateRelativeSpace(Container parent, int spaceAvailable, float relativeTotal)


getAlignment

public float getAlignment()
Gets the alignment of the components on the opposite axis.
Returns:
the alignment


getAxis

public int getAxis()
Gets the layout axis.
Returns:
the layout axis


getBorderGap

public int getBorderGap()
Gets the initial gap. This gap is used before the leading component and after the trailing component.
Returns:
the leading/trailing gap


getConstraints

public Float getConstraints(Component component)
Gets the constraints for the specified component.
Parameters:
component - the component to be queried
Returns:
the constraint for the specified component, or null if component is null or is not present in this layout


getDimension

private Dimension getDimension(Component component, int type)


getFillGap

public int getFillGap()
Gets the fill gap amount.
Returns:
the fill gap value


getGap

public int getGap()
Gets the gap between components.
Returns:
the gap between components


getLayoutAlignmentX

public float getLayoutAlignmentX(Container parent)
Returns the alignment along the x axis. Use center alignment.


getLayoutAlignmentY

public float getLayoutAlignmentY(Container parent)
Returns the alignment along the y axis. Use center alignment.


getLayoutSize

private Dimension getLayoutSize(Container parent, int type)


getLocationX

private int getLocationX(Component component, int width)


getLocationY

private int getLocationY(Component component, int height)


getRoundingPolicy

public int getRoundingPolicy()
Gets the rounding policy.
Returns:
the rounding policy


getVisibleComponents

private int getVisibleComponents(Container container)


invalidateLayout

public void invalidateLayout(Container target)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.


isFill

public boolean isFill()
Gets the fill property for the component size on the opposite edge.
Returns:
the fill property


layoutContainer

public void layoutContainer(Container parent)
Lays out the specified container using this layout.

This method reshapes the components in the specified target container in order to satisfy the constraints of the RelativeLayout object.

The grid layout manager determines the size of individual components by dividing the free space in the container into equal-sized portions according to the number of rows and columns in the layout. The container's free space equals the container's size minus any insets and any specified horizontal or vertical gap. All components in a grid layout are given the same size.

Parameters:
target - the container in which to do the layout
See Also:
Container
Container.doLayout


layoutContainerHorizontally

private void layoutContainerHorizontally(Container parent)


layoutContainerVertically

private void layoutContainerVertically(Container parent)


maximumLayoutSize

public Dimension maximumLayoutSize(Container target)
There is no maximum.


minimumLayoutSize

public Dimension minimumLayoutSize(Container parent)
Determines the minimum size of the container argument using this column layout.

The minimum width of a grid layout is the largest minimum width of each column in the container, plus the horizontal padding times the number of columns minus one, plus the left and right insets of the target container.

The minimum height of a column layout is the largest minimum height of each row in the container, plus the vertical padding times the number of rows minus one, plus the top and bottom insets of the target container.

Parameters:
target - the container in which to do the layout
Returns:
the minimum dimensions needed to lay out the subcomponents of the specified container
See Also:
RelativeLayout.preferredLayoutSize
Container.doLayout


preferredLayoutSize

public Dimension preferredLayoutSize(Container parent)
Determines the preferred size of the container argument using this column layout.

The preferred width of a column layout is the largest preferred width of each column in the container, plus the horizontal padding times the number of columns minus one, plus the left and right insets of the target container.

The preferred height of a column layout is the largest preferred height of each row in the container, plus the vertical padding times the number of rows minus one, plus the top and bottom insets of the target container.

Parameters:
target - the container in which to do the layout
Returns:
the preferred dimensions to lay out the subcomponents of the specified container
See Also:
RelativeLayout.minimumLayoutSize
Container.getPreferredSize


removeLayoutComponent

public void removeLayoutComponent(Component comp)
Removes the specified component from the layout.
Parameters:
comp - the component to be removed


setAlignment

public void setAlignment(float alignment)


setAxis

public void setAxis(int axis)
Sets the layout axis
Parameters:
axis - the layout axis


setBorderGap

public void setBorderGap(int borderGap)
Sets the initial gap. This gap is used before the leading component and after the trailing component. The default is set to the gap.
Parameters:
borderGap - the leading/trailing gap


setFill

public void setFill(boolean fill)
Change size of relative components to fill the space available For X-AXIS aligned components the height will be filled. For Y-AXIS aligned components the width will be filled.


setFillGap

public void setFillGap(int fillGap)
Specify the number of pixels by which the fill size is decreased when setFill(true) has been specified.


setGap

public void setGap(int gap)
Sets the gap between components to the specified value.
Parameters:
gap - the gap between components


setRoundingPolicy

public void setRoundingPolicy(int roundingPolicy)
Specify the rounding policy to be used when all the avialable pixels have not been allocated to a component. DO_NOTHING FIRST - extra pixels added to the first relative component LAST - extra pixels added to the last relative component LARGEST (default) - extra pixels added to the larger relative component EQUAL - a single pixel is added to each relative component (until pixels are used up)


toString

public String toString()
Returns the string representation of this column layout's values.
Returns:
a string representation of this grid layout


 

Groovy Documentation