MDateSelector

mseries.utils
Class FormLayout

java.lang.Object
  extended by mseries.utils.FormLayout
All Implemented Interfaces:
LayoutManager, LayoutManager2, Serializable
Direct Known Subclasses:
FormCellLayout

public class FormLayout
extends Object
implements LayoutManager2, Serializable

The FormLayout class is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the same size. Each FormLayout object maintains a dynamic rectangular grid of cells, with each component occupying one cell, called its display area.

Each component managed by a form layout is associated with an instance of FormConstraints that specifies how the component is laid out within its display area.

How a FormLayout object places a set of components depends on the FormConstraints object associated with each component, and on the minimum size and the preferred size of the components' containers.

To use a form layout effectively, you must customize one or more of the FormConstraints objects that are associated with its components. You customize a FormConstraints object by setting one or more of its instance variables:

FormConstraints.gridx, FormConstraints.gridy
Specifies the cell at the upper left of the component's display area, where the upper-left-most cell has address gridx = 0, gridy = 0. Use FormConstraints.RELATIVE (the default value) to specify that the component be just placed just to the right of (for gridx) or just below (for gridy) the component that was added to the container just before this component was added.
FormConstraints.insets
Specifies the component's external padding, the minimum amount of space between the component and the edges of its display area. This is defaulted to four pixels on each side.
FormConstraints.anchor
Used when the component is smaller than its display area to determine where (within the display area) to place the component. Valid values are FormConstraints.CENTER (the default), FormConstraints.NORTH, FormConstraints.NORTHEAST, FormConstraints.EAST, FormConstraints.SOUTHEAST, FormConstraints.SOUTH, FormConstraints.SOUTHWEST, FormConstraints.WEST, and FormConstraints.NORTHWEST.
FormConstraints.spansColumns
Specifies the component can span across column boundaries and the preferred width width will be used if the container (not the column) is wide enough

The following figure shows 16 components managed by a form layout:


Here is the code that implements the example shown above, the creation of the components and setting of the border have been omitted.


        JPanel pnlWeight = new JPanel();

        pnlWeight.setLayout(new FormLayout());


        FormConstraints constraints = new  FormConstraints();
        constraints.gridx       = 0;
        constraints.gridy       = 0;

        // Gross Weight
        constraints.anchor      = FormConstraints.EAST;
        pnlWeight.add(lblGrossWeightMand,constraints);

        constraints.anchor      = FormConstraints.WEST;
        constraints.gridx++;
        pnlWeight.add(tfGrossWeight,constraints);

        // UOM2
        constraints.anchor      = FormConstraints.EAST;
        constraints.gridx++;
        pnlWeight.add(lblUOM2,constraints);

        constraints.anchor      = FormConstraints.WEST;
        constraints.gridx++;
        pnlWeight.add(cmbUOM2,constraints);

        // Net Weight
        constraints.anchor      = FormConstraints.EAST;
        constraints.gridx       = 0;
        constraints.gridy++;
        pnlWeight.add(lblNetWeightMand,constraints);

        constraints.anchor      = FormConstraints.WEST;
        constraints.gridx++;
        pnlWeight.add(tfNetWeight,constraints);

        // UOM 3
        constraints.anchor      = FormConstraints.EAST;
        constraints.gridx++;
        pnlWeight.add(lblUOM3,constraints);

        constraints.anchor      = FormConstraints.WEST;
        constraints.gridx++;
        pnlWeight.add(cmbUOM3,constraints);


        // Tare Weight
        constraints.anchor      = FormConstraints.EAST;
        constraints.gridx       = 0;
        constraints.gridy++;
        pnlWeight.add(lblTareWeight,constraints);

        constraints.anchor      = FormConstraints.WEST;
        constraints.gridx++;
        pnlWeight.add(tfTareWeight,constraints);

        // UOM 4
        constraints.anchor      = FormConstraints.EAST;
        constraints.gridx++;
        pnlWeight.add(lblUOM4,constraints);

        constraints.anchor      = FormConstraints.WEST;
        constraints.gridx++;
        pnlWeight.add(cmbUOM4,constraints);

        // Average Weight
        constraints.anchor      = FormConstraints.EAST;
        constraints.gridx       = 0;
        constraints.gridy++;
        pnlWeight.add(lblAverageWeight,constraints);

        constraints.anchor      = FormConstraints.WEST;
        constraints.gridx++;
        pnlWeight.add(tfAverageWeight,constraints);


        // UOM 5
        constraints.anchor      = FormConstraints.EAST;
        constraints.gridx++;
        pnlWeight.add(lblUOM5,constraints);

        constraints.anchor      = FormConstraints.WEST;
        constraints.gridx++;
        pnlWeight.add(cmbUOM5,constraints);




See Also:
FormConstraints, Serialized Form

Field Summary
protected  Hashtable<Component,FormConstraints> comptable
          This hashtable maintains the association between a component and its gridbox constraints.
protected  FormConstraints defaultConstraints
          This field holds a gridbox constraints instance containing the default values, so if a component does not have gridbox constraints associated with it, then the component will be assigned a copy of the defaultConstraints.
protected  mseries.utils.FormLayoutInfo info
          One FormLayoutInfo object is maintained for the layout manager that contains the working attributes for the whole layout.
protected static int MAXGRIDSIZE
          The maximum number of grid positions (both horizontally and vertically) that can be laid out by the form layout.
protected static int MINHEIGHT
          The smallest size (height) to make a component in each cell
protected static int MINWIDTH
          The smallest size (width) to make a component in each cell
 
Constructor Summary
FormLayout()
          Creates a form layout manager.
 
Method Summary
 void addLayoutComponent(Component comp, Object constraints)
          Adds the specified component to the layout, using the specified constraint object.
 void addLayoutComponent(String name, Component comp)
          Adds the specified component with the specified name to the layout.
protected  void applyGravity(FormConstraints constraints, Rectangle r)
          Relocates the component inside its cell according to the anchor constraint
protected  void calculateGridSize(Container parent, Dimension size)
          Calculates the location and size of each component in the container, according to the in built rules and the constraints of each component this is the job of the layout manager.
 FormConstraints getConstraints(Component comp)
          Gets the constraints for the specified component.
 mseries.utils.FormLayoutInfo getInfo()
           
 float getLayoutAlignmentX(Container parent)
          Returns the alignment along the x axis.
 float getLayoutAlignmentY(Container parent)
          Returns the alignment along the y axis.
 void invalidateLayout(Container target)
          Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
 void layoutContainer(Container parent)
          Lays out the specified container using this form layout.
protected  FormConstraints lookupConstraints(Component comp)
          Retrieves the constraints for the specified component.
 Dimension maximumLayoutSize(Container target)
          Returns the maximum dimensions for this layout given the components in the specified target container.
 Dimension minimumLayoutSize(Container parent)
          Determines the minimum size of the target container using this form layout.
 Dimension preferredLayoutSize(Container parent)
          Determines the preferred size of the target container using this form layout.
 void removeLayoutComponent(Component comp)
          Removes the specified component from this layout.
protected  void setComponentBounds(Container parent)
          Set the bounds on the components based on the values calculated in the calculateGridSize method
 void setConstraints(Component comp, FormConstraints constraints)
          Sets the constraints for the specified component in this layout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXGRIDSIZE

protected static final int MAXGRIDSIZE
The maximum number of grid positions (both horizontally and vertically) that can be laid out by the form layout. Current value is 20

See Also:
Constant Field Values

MINWIDTH

protected static final int MINWIDTH
The smallest size (width) to make a component in each cell

See Also:
Constant Field Values

MINHEIGHT

protected static final int MINHEIGHT
The smallest size (height) to make a component in each cell

See Also:
Constant Field Values

comptable

protected Hashtable<Component,FormConstraints> comptable
This hashtable maintains the association between a component and its gridbox constraints. The Keys in comptable are the components and the values are the instances of FormConstraints.

See Also:
FormConstraints

defaultConstraints

protected FormConstraints defaultConstraints
This field holds a gridbox constraints instance containing the default values, so if a component does not have gridbox constraints associated with it, then the component will be assigned a copy of the defaultConstraints.

See Also:
getConstraints(Component), setConstraints(Component, FormConstraints), lookupConstraints(Component)

info

protected mseries.utils.FormLayoutInfo info
One FormLayoutInfo object is maintained for the layout manager that contains the working attributes for the whole layout.

Constructor Detail

FormLayout

public FormLayout()
Creates a form layout manager.

Method Detail

getInfo

public mseries.utils.FormLayoutInfo getInfo()
Returns:
the info object used by this instance of the layout manager

addLayoutComponent

public void addLayoutComponent(String name,
                               Component comp)
Adds the specified component with the specified name to the layout.

Specified by:
addLayoutComponent in interface LayoutManager
Parameters:
name - the name of the component.
comp - the component to be added.

addLayoutComponent

public void addLayoutComponent(Component comp,
                               Object constraints)
Adds the specified component to the layout, using the specified constraint object.

Specified by:
addLayoutComponent in interface LayoutManager2
Parameters:
comp - the component to be added.
constraints - an object that determines how the component is added to the layout.

removeLayoutComponent

public void removeLayoutComponent(Component comp)
Removes the specified component from this layout.

Most applications do not call this method directly.

Specified by:
removeLayoutComponent in interface LayoutManager
Parameters:
comp - the component to be removed.
See Also:
Container.remove(java.awt.Component), Container.removeAll()

setConstraints

public void setConstraints(Component comp,
                           FormConstraints constraints)
Sets the constraints for the specified component in this layout.

Parameters:
comp - the component to be modified.
constraints - the constraints to be applied.

getConstraints

public FormConstraints getConstraints(Component comp)
Gets the constraints for the specified component. A copy of the actual FormConstraints object is returned.

Parameters:
comp - the component to be queried.
Returns:
the constraint for the specified component in this form layout; a copy of the actual constraint object is returned.

lookupConstraints

protected FormConstraints lookupConstraints(Component comp)
Retrieves the constraints for the specified component. The return value is not a copy, but is the actual FormConstraints object used by the layout mechanism.

Parameters:
comp - the component to be queried
Returns:
the contraints for the specified component.

layoutContainer

public void layoutContainer(Container parent)
Lays out the specified container using this form layout. This method reshapes components in the specified container in order to satisfy the contraints of this FormLayout object.

Most applications do not call this method directly.

Specified by:
layoutContainer in interface LayoutManager
Parameters:
parent - the container in which to do the layout.
See Also:
Container, Container.doLayout()

calculateGridSize

protected void calculateGridSize(Container parent,
                                 Dimension size)
Calculates the location and size of each component in the container, according to the in built rules and the constraints of each component this is the job of the layout manager.


setComponentBounds

protected void setComponentBounds(Container parent)
Set the bounds on the components based on the values calculated in the calculateGridSize method


applyGravity

protected void applyGravity(FormConstraints constraints,
                            Rectangle r)
Relocates the component inside its cell according to the anchor constraint

Parameters:
constraints - the FormConstraints for the cell
r - the rectangle speifying the bounds of the cell

preferredLayoutSize

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

Most applications do not call this method directly.

Specified by:
preferredLayoutSize in interface LayoutManager
Parameters:
parent - the container in which to do the layout.
See Also:
Container.getPreferredSize()

minimumLayoutSize

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

Most applications do not call this method directly.

Specified by:
minimumLayoutSize in interface LayoutManager
Parameters:
parent - the container in which to do the layout.
See Also:
Container.doLayout()

maximumLayoutSize

public Dimension maximumLayoutSize(Container target)
Returns the maximum dimensions for this layout given the components in the specified target container.

Specified by:
maximumLayoutSize in interface LayoutManager2
Parameters:
target - the component which needs to be laid out
See Also:
Container, minimumLayoutSize(Container), preferredLayoutSize(Container)

getLayoutAlignmentX

public float getLayoutAlignmentX(Container parent)
Returns the alignment along the x axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

Specified by:
getLayoutAlignmentX in interface LayoutManager2

getLayoutAlignmentY

public float getLayoutAlignmentY(Container parent)
Returns the alignment along the y axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

Specified by:
getLayoutAlignmentY in interface LayoutManager2

invalidateLayout

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

Specified by:
invalidateLayout in interface LayoutManager2

MDateSelector

Copyright © 2001-2007 M Newstead. All Rights Reserved.