ObjectFormLayout
public class FormLayout
FormLayout provides a simple means of controlling layout which is sufficiently powerful to enable many layouts to be achieved with few programming statements. Four types of constraint are provided:
Unlike other layout managers, the FormLayout constructor requires a parameter, the container it is responsible for. With this layout manager, components should not be added to the container by the programmer; instead, calls should be made to the layout manager indicating how the children are to be constrained, and the layout manager will itself tell the container of the children it has gained. The FormLayout object needs to be told of these constraints with methods:
| Methods: | Actions: |
setLeftAnchor, setRightAnchor,
setTopAnchor, setBottomAnchor
| Fixed distance from an edge of the container |
setLeftRelative, setRightRelative,
setTopRelative, setBottomRelative
| Fixed distance relative to a neighbouring component |
setLeftAlign, setRightAlign, setTopAlign,
setBottomAlign
| One edge aligned with another component |
setHorizontalAnchor, setVerticalAnchor
| Fractional position within the available width or height |
The inspiration for this design came from the form widget in X Windows. There is also a FormLayout in SWT developed by IBM and maintained now by Eclipse, which is more sophisticated and flexible, but requires more lines of code to insert the components. It is also similar to Sun's SpringLayout, but with fewer features and requiring rather less code.
The size of the container is determined from the components placed with respect to the edges or the other components in the container. Components placed at a fraction of the width or height of the container are not considered in determining size; they rely on space being present as a result of the size of the other components.
Relative placement can be made with respect to more than one neighbour on the left or top side. FormLayout will determine which takes the greater amount of space and set this position for the component.

Container cpane = getContentPane();
FormLayout form = new FormLayout(cpane);
JLabel lab1 = new JLabel("Name: ") ;
form.setTopAnchor( lab1, 3 );
form.setLeftAnchor( lab1, 4 );
JLabel lab2 = new JLabel("Address: ");
JTextField name = new JTextField(" ",50);
form.setLeftRelative( name, lab1, 3 );
form.setLeftRelative( name, lab2, 3 );
form.setTopAnchor( name, 3 );
form.setLeftAnchor( lab2, 4 );
form.setTopRelative( lab2, name, 3 );
JTextArea addr = new JTextArea(" ",3,50 );
JScrollPane jsp = new JScrollPane( addr,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
form.setLeftAlign( jsp, name );
form.setTopRelative( jsp, name, 3 );
form.setRightAnchor( jsp, 4 );
JLabel lab4 = new JLabel("Adult ");
form.setLeftAlign( lab4, name );
form.setTopRelative( lab4, jsp, 3 );
JTextField adults = new JTextField(" ",3);
form.setLeftRelative( adults, lab4, 3 );
form.setTopRelative( adults, jsp, 3 );
JLabel Ch = new JLabel("Child");
form.setLeftRelative( Ch, adults, 16 );
form.setTopRelative( Ch, jsp, 3 );
JTextField children = new JTextField(" ",3);
form.setLeftRelative( children, Ch, 3 );
form.setTopRelative( children, jsp, 3 );
JLabel Con = new JLabel("Concession");
form.setLeftRelative( Con, children, 16 );
form.setTopRelative( Con, jsp, 3 );
JTextField concess = new JTextField(" ",3);
form.setLeftRelative( concess, Con, 3 );
form.setTopRelative( concess, jsp, 3 );
JButton accept = new JButton("Accept");
form.setHorizontalAnchor( accept, 0.2 );
form.setTopRelative( accept, adults, 3 );
JButton reject = new JButton("Reject");
form.setHorizontalAnchor( reject, 0.5 );
form.setTopAlign( reject, accept );
accept.addActionListener( this );
reject.addActionListener( this );
JButton quit = new JButton("Exit");
form.setHorizontalAnchor( quit, 0.8 );
form.setTopAlign( quit, accept );
form.setBottomAnchor( quit, 5 );
quit.addActionListener( this );
pack();
FormLayout does not have any concept of filling available space, so there are
situations where it will not give as neat appearance as GridBag, such as with
JScrollPanes. There are also occasions after pack() has been called when
FormLayout requests the preferred size of the components within it, and resizes
the container accordingly. If the size reported by a component is incorrect,
FormLayout will have no way of detecting this. It may be necessary to call
pack() a second time in these situations.
| Field Summary | |
|---|---|
static String |
EOL
End-of-line character(s) on the current machine. |
| Constructor Summary | |
|---|---|
FormLayout(Container c)
Construct a FormLayout object, telling the layout manager which container it is responsible for. |
|
| Method Summary | |
|---|---|
void |
addLayoutComponent(String name,
Component comp)
If the layout manager uses a per-component string, adds the component "comp" to the layout, associating it with the string specified by "name". |
String |
fetchPlacements()
For debugging purposes, it can be useful to fetch the size and placement of each component controlled by this FormLayout object. |
void |
layoutContainer(Container parent)
Lays out the specified container. |
Dimension |
minimumLayoutSize(Container parent)
Calculates the minimum size dimensions for the specified container, given the components it contains. |
Dimension |
preferredLayoutSize(Container parent)
Calculates the preferred size dimensions for the specified container, given the components it contains. |
void |
removeLayoutComponent(Component comp)
Removes the specified component from the layout. |
void |
setBottomAlign(Component thisobj,
Component botnbr)
Sets the component to which thisobj will be aligned on the bottom side. |
void |
setBottomAnchor(Component thisobj,
int dist)
Sets the distance in pixels between the bottom edge of the component and the bottom container margin. |
void |
setBottomRelative(Component thisobj,
Component botnbr,
int gap)
Sets the distance the component will be from a neighbour on its bottom side. |
void |
setHorizontalAnchor(Component thisobj,
double dist)
Sets the horizontal position of the centre of the component. |
void |
setLeftAlign(Component thisobj,
Component leftnbr)
Sets the component to which thisobj will be aligned on the left side. |
void |
setLeftAnchor(Component thisobj,
int dist)
Sets the distance in pixels between the left edge of the component and the left container margin. |
void |
setLeftRelative(Component thisobj,
Component leftnbr,
int gap)
Sets the distance the component will be from a neighbour on its left side. |
void |
setRightAlign(Component thisobj,
Component rightnbr)
Sets the component to which thisobj will be aligned on the right side. |
void |
setRightAnchor(Component thisobj,
int dist)
Sets the distance in pixels between the right edge of the component and the right container margin. |
void |
setRightRelative(Component thisobj,
Component rightnbr,
int gap)
Sets the distance the component will be from a neighbour on its right side. |
void |
setTopAlign(Component thisobj,
Component topnbr)
Sets the component to which thisobj will be aligned on the top side. |
void |
setTopAnchor(Component thisobj,
int dist)
Sets the distance in pixels between the top edge of the component and the top container margin. |
void |
setTopRelative(Component thisobj,
Component topnbr,
int gap)
Sets the distance the component will be from a neighbour on its top side. |
void |
setVerticalAnchor(Component thisobj,
double dist)
Sets the vertical position of the centre of the component. |
| Methods inherited from class Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static String EOL
| Constructor Detail |
|---|
public FormLayout(Container c)
| Method Detail |
|---|
public void addLayoutComponent(String name,
Component comp)
Actually, this does nothing. Components must be added by setting constraints. We will tell the container about the additions, so there is no need for the container to tell us when something is added.
addLayoutComponent in interface LayoutManagerpublic String fetchPlacements()
public void layoutContainer(Container parent)
layoutContainer in interface LayoutManagerRuntimeException - Is thrown if the parameter does not match
the container being laid out by this manager.public Dimension minimumLayoutSize(Container parent)
minimumLayoutSize in interface LayoutManagerRuntimeException - can occur because the parent parameter is
not the container which the layout manager is responsible for, or
because the constraints are not consistent, eg. because they are
circular or because a component given as a neighbour has not been
added to the layout manager.public Dimension preferredLayoutSize(Container parent)
preferredLayoutSize in interface LayoutManagerRuntimeException - can occur because the parent parameter is
not the container which the layout manager is responsible for, or
because the constraints are not consistent, eg. because they are
circular or because a component given as a neighbour has not been
added to the layout manager.public void removeLayoutComponent(Component comp)
removeLayoutComponent in interface LayoutManager
public void setBottomAlign(Component thisobj,
Component botnbr)
public void setBottomAnchor(Component thisobj,
int dist)
public void setBottomRelative(Component thisobj,
Component botnbr,
int gap)
public void setHorizontalAnchor(Component thisobj,
double dist)
dist - is a fraction of the container dimension.
public void setLeftAlign(Component thisobj,
Component leftnbr)
public void setLeftAnchor(Component thisobj,
int dist)
public void setLeftRelative(Component thisobj,
Component leftnbr,
int gap)
public void setRightAlign(Component thisobj,
Component rightnbr)
public void setRightAnchor(Component thisobj,
int dist)
public void setRightRelative(Component thisobj,
Component rightnbr,
int gap)
public void setTopAlign(Component thisobj,
Component topnbr)
public void setTopAnchor(Component thisobj,
int dist)
public void setTopRelative(Component thisobj,
Component topnbr,
int gap)
public void setVerticalAnchor(Component thisobj,
double dist)
dist - is a fraction of the container dimension.