java.awt
Class FlowLayout

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.awt.FlowLayout
All Implemented Interfaces:
LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager , Serializable sample code for java.io.Serializable definition code for java.io.Serializable

public class FlowLayout
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager , Serializable sample code for java.io.Serializable definition code for java.io.Serializable

A flow layout arranges components in a directional flow, much like lines of text in a paragraph. The flow direction is determined by the container's componentOrientation property and may be one of two values:

Flow layouts are typically used to arrange buttons in a panel. It arranges buttons horizontally until no more buttons fit on the same line. The line alignment is determined by the align property. The possible values are:

For example, the following picture shows an applet using the flow layout manager (its default layout manager) to position three buttons:

Graphic of Layout for Three Buttons

Here is the code for this applet:


 import java.awt.*;
 import java.applet.Applet;

 public class myButtons extends Applet {
     Button button1, button2, button3;
     public void init() {
         button1 = new Button("Ok");
         button2 = new Button("Open");
         button3 = new Button("Close");
         add(button1);
         add(button2);
         add(button3);
     }
 }
 

A flow layout lets each component assume its natural (preferred) size.

Since:
JDK1.0
See Also:
ComponentOrientation sample code for java.awt.ComponentOrientation definition code for java.awt.ComponentOrientation , Serialized Form

Field Summary
static int CENTER sample code for java.awt.FlowLayout.CENTER definition code for java.awt.FlowLayout.CENTER
          This value indicates that each row of components should be centered.
static int LEADING sample code for java.awt.FlowLayout.LEADING definition code for java.awt.FlowLayout.LEADING
          This value indicates that each row of components should be justified to the leading edge of the container's orientation, for example, to the left in left-to-right orientations.
static int LEFT sample code for java.awt.FlowLayout.LEFT definition code for java.awt.FlowLayout.LEFT
          This value indicates that each row of components should be left-justified.
static int RIGHT sample code for java.awt.FlowLayout.RIGHT definition code for java.awt.FlowLayout.RIGHT
          This value indicates that each row of components should be right-justified.
static int TRAILING sample code for java.awt.FlowLayout.TRAILING definition code for java.awt.FlowLayout.TRAILING
          This value indicates that each row of components should be justified to the trailing edge of the container's orientation, for example, to the right in left-to-right orientations.
 
Constructor Summary
FlowLayout sample code for java.awt.FlowLayout.FlowLayout() definition code for java.awt.FlowLayout.FlowLayout() ()
          Constructs a new FlowLayout with a centered alignment and a default 5-unit horizontal and vertical gap.
FlowLayout sample code for java.awt.FlowLayout.FlowLayout(int) definition code for java.awt.FlowLayout.FlowLayout(int) (int align)
          Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap.
FlowLayout sample code for java.awt.FlowLayout.FlowLayout(int, int, int) definition code for java.awt.FlowLayout.FlowLayout(int, int, int) (int align, int hgap, int vgap)
          Creates a new flow layout manager with the indicated alignment and the indicated horizontal and vertical gaps.
 
Method Summary
 void addLayoutComponent sample code for java.awt.FlowLayout.addLayoutComponent(java.lang.String, java.awt.Component) definition code for java.awt.FlowLayout.addLayoutComponent(java.lang.String, java.awt.Component) (String sample code for java.lang.String definition code for java.lang.String  name, Component sample code for java.awt.Component definition code for java.awt.Component  comp)
          Adds the specified component to the layout.
 int getAlignment sample code for java.awt.FlowLayout.getAlignment() definition code for java.awt.FlowLayout.getAlignment() ()
          Gets the alignment for this layout.
 int getHgap sample code for java.awt.FlowLayout.getHgap() definition code for java.awt.FlowLayout.getHgap() ()
          Gets the horizontal gap between components and between the components and the borders of the Container
 int getVgap sample code for java.awt.FlowLayout.getVgap() definition code for java.awt.FlowLayout.getVgap() ()
          Gets the vertical gap between components and between the components and the borders of the Container.
 void layoutContainer sample code for java.awt.FlowLayout.layoutContainer(java.awt.Container) definition code for java.awt.FlowLayout.layoutContainer(java.awt.Container) (Container sample code for java.awt.Container definition code for java.awt.Container  target)
          Lays out the container.
 Dimension sample code for java.awt.Dimension definition code for java.awt.Dimension minimumLayoutSize sample code for java.awt.FlowLayout.minimumLayoutSize(java.awt.Container) definition code for java.awt.FlowLayout.minimumLayoutSize(java.awt.Container) (Container sample code for java.awt.Container definition code for java.awt.Container  target)
          Returns the minimum dimensions needed to layout the visible components contained in the specified target container.
 Dimension sample code for java.awt.Dimension definition code for java.awt.Dimension preferredLayoutSize sample code for java.awt.FlowLayout.preferredLayoutSize(java.awt.Container) definition code for java.awt.FlowLayout.preferredLayoutSize(java.awt.Container) (Container sample code for java.awt.Container definition code for java.awt.Container  target)
          Returns the preferred dimensions for this layout given the visible components in the specified target container.
 void removeLayoutComponent sample code for java.awt.FlowLayout.removeLayoutComponent(java.awt.Component) definition code for java.awt.FlowLayout.removeLayoutComponent(java.awt.Component) (Component sample code for java.awt.Component definition code for java.awt.Component  comp)
          Removes the specified component from the layout.
 void setAlignment sample code for java.awt.FlowLayout.setAlignment(int) definition code for java.awt.FlowLayout.setAlignment(int) (int align)
          Sets the alignment for this layout.
 void setHgap sample code for java.awt.FlowLayout.setHgap(int) definition code for java.awt.FlowLayout.setHgap(int) (int hgap)
          Sets the horizontal gap between components and between the components and the borders of the Container.
 void setVgap sample code for java.awt.FlowLayout.setVgap(int) definition code for java.awt.FlowLayout.setVgap(int) (int vgap)
          Sets the vertical gap between components and between the components and the borders of the Container.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.awt.FlowLayout.toString() definition code for java.awt.FlowLayout.toString() ()
          Returns a string representation of this FlowLayout object and its values.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

LEFT sample code for java.awt.FlowLayout.LEFT

public static final int LEFT
This value indicates that each row of components should be left-justified.

See Also:
Constant Field Values

CENTER sample code for java.awt.FlowLayout.CENTER

public static final int CENTER
This value indicates that each row of components should be centered.

See Also:
Constant Field Values

RIGHT sample code for java.awt.FlowLayout.RIGHT

public static final int RIGHT
This value indicates that each row of components should be right-justified.

See Also:
Constant Field Values

LEADING sample code for java.awt.FlowLayout.LEADING

public static final int LEADING
This value indicates that each row of components should be justified to the leading edge of the container's orientation, for example, to the left in left-to-right orientations.

Since:
1.2 Package-private pending API change approval
See Also:
Component.getComponentOrientation() sample code for java.awt.Component.getComponentOrientation() definition code for java.awt.Component.getComponentOrientation() , ComponentOrientation sample code for java.awt.ComponentOrientation definition code for java.awt.ComponentOrientation , Constant Field Values

TRAILING sample code for java.awt.FlowLayout.TRAILING

public static final int TRAILING
This value indicates that each row of components should be justified to the trailing edge of the container's orientation, for example, to the right in left-to-right orientations.

Since:
1.2 Package-private pending API change approval
See Also:
Component.getComponentOrientation() sample code for java.awt.Component.getComponentOrientation() definition code for java.awt.Component.getComponentOrientation() , ComponentOrientation sample code for java.awt.ComponentOrientation definition code for java.awt.ComponentOrientation , Constant Field Values
Constructor Detail

FlowLayout sample code for java.awt.FlowLayout() definition code for java.awt.FlowLayout()

public FlowLayout()
Constructs a new FlowLayout with a centered alignment and a default 5-unit horizontal and vertical gap.


FlowLayout sample code for java.awt.FlowLayout(int) definition code for java.awt.FlowLayout(int)

public FlowLayout(int align)
Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap. The value of the alignment argument must be one of FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.

Parameters:
align - the alignment value

FlowLayout sample code for java.awt.FlowLayout(int, int, int) definition code for java.awt.FlowLayout(int, int, int)

public FlowLayout(int align,
                  int hgap,
                  int vgap)
Creates a new flow layout manager with the indicated alignment and the indicated horizontal and vertical gaps.

The value of the alignment argument must be one of FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.

Parameters:
align - the alignment value
hgap - the horizontal gap between components and between the components and the borders of the Container
vgap - the vertical gap between components and between the components and the borders of the Container
Method Detail

getAlignment sample code for java.awt.FlowLayout.getAlignment() definition code for java.awt.FlowLayout.getAlignment()

public int getAlignment()
Gets the alignment for this layout. Possible values are FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.

Returns:
the alignment value for this layout
Since:
JDK1.1
See Also:
setAlignment(int) sample code for java.awt.FlowLayout.setAlignment(int) definition code for java.awt.FlowLayout.setAlignment(int)

setAlignment sample code for java.awt.FlowLayout.setAlignment(int) definition code for java.awt.FlowLayout.setAlignment(int)

public void setAlignment(int align)
Sets the alignment for this layout. Possible values are

Parameters:
align - one of the alignment values shown above
Since:
JDK1.1
See Also:
getAlignment() sample code for java.awt.FlowLayout.getAlignment() definition code for java.awt.FlowLayout.getAlignment()

getHgap sample code for java.awt.FlowLayout.getHgap() definition code for java.awt.FlowLayout.getHgap()

public int getHgap()
Gets the horizontal gap between components and between the components and the borders of the Container

Returns:
the horizontal gap between components and between the components and the borders of the Container
Since:
JDK1.1
See Also:
setHgap(int) sample code for java.awt.FlowLayout.setHgap(int) definition code for java.awt.FlowLayout.setHgap(int)

setHgap sample code for java.awt.FlowLayout.setHgap(int) definition code for java.awt.FlowLayout.setHgap(int)

public void setHgap(int hgap)
Sets the horizontal gap between components and between the components and the borders of the Container.

Parameters:
hgap - the horizontal gap between components and between the components and the borders of the Container
Since:
JDK1.1
See Also:
getHgap() sample code for java.awt.FlowLayout.getHgap() definition code for java.awt.FlowLayout.getHgap()

getVgap sample code for java.awt.FlowLayout.getVgap() definition code for java.awt.FlowLayout.getVgap()

public int getVgap()
Gets the vertical gap between components and between the components and the borders of the Container.

Returns:
the vertical gap between components and between the components and the borders of the Container
Since:
JDK1.1
See Also:
setVgap(int) sample code for java.awt.FlowLayout.setVgap(int) definition code for java.awt.FlowLayout.setVgap(int)

setVgap sample code for java.awt.FlowLayout.setVgap(int) definition code for java.awt.FlowLayout.setVgap(int)

public void setVgap(int vgap)
Sets the vertical gap between components and between the components and the borders of the Container.

Parameters:
vgap - the vertical gap between components and between the components and the borders of the Container
Since:
JDK1.1
See Also:
getVgap() sample code for java.awt.FlowLayout.getVgap() definition code for java.awt.FlowLayout.getVgap()

addLayoutComponent sample code for java.awt.FlowLayout.addLayoutComponent(java.lang.String, java.awt.Component) definition code for java.awt.FlowLayout.addLayoutComponent(java.lang.String, java.awt.Component)

public void addLayoutComponent(String sample code for java.lang.String definition code for java.lang.String  name,
                               Component sample code for java.awt.Component definition code for java.awt.Component  comp)
Adds the specified component to the layout. Not used by this class.

Specified by:
addLayoutComponent sample code for java.awt.LayoutManager.addLayoutComponent(java.lang.String, java.awt.Component) definition code for java.awt.LayoutManager.addLayoutComponent(java.lang.String, java.awt.Component) in interface LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager
Parameters:
name - the name of the component
comp - the component to be added

removeLayoutComponent sample code for java.awt.FlowLayout.removeLayoutComponent(java.awt.Component) definition code for java.awt.FlowLayout.removeLayoutComponent(java.awt.Component)

public void removeLayoutComponent(Component sample code for java.awt.Component definition code for java.awt.Component  comp)
Removes the specified component from the layout. Not used by this class.

Specified by:
removeLayoutComponent sample code for java.awt.LayoutManager.removeLayoutComponent(java.awt.Component) definition code for java.awt.LayoutManager.removeLayoutComponent(java.awt.Component) in interface LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager
Parameters:
comp - the component to remove
See Also:
Container.removeAll() sample code for java.awt.Container.removeAll() definition code for java.awt.Container.removeAll()

preferredLayoutSize sample code for java.awt.FlowLayout.preferredLayoutSize(java.awt.Container) definition code for java.awt.FlowLayout.preferredLayoutSize(java.awt.Container)

public Dimension sample code for java.awt.Dimension definition code for java.awt.Dimension  preferredLayoutSize(Container sample code for java.awt.Container definition code for java.awt.Container  target)
Returns the preferred dimensions for this layout given the visible components in the specified target container.

Specified by:
preferredLayoutSize sample code for java.awt.LayoutManager.preferredLayoutSize(java.awt.Container) definition code for java.awt.LayoutManager.preferredLayoutSize(java.awt.Container) in interface LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager
Parameters:
target - the container that needs to be laid out
Returns:
the preferred dimensions to lay out the subcomponents of the specified container
See Also:
Container sample code for java.awt.Container definition code for java.awt.Container , minimumLayoutSize(java.awt.Container) sample code for java.awt.FlowLayout.minimumLayoutSize(java.awt.Container) definition code for java.awt.FlowLayout.minimumLayoutSize(java.awt.Container) , Container.getPreferredSize() sample code for java.awt.Container.getPreferredSize() definition code for java.awt.Container.getPreferredSize()

minimumLayoutSize sample code for java.awt.FlowLayout.minimumLayoutSize(java.awt.Container) definition code for java.awt.FlowLayout.minimumLayoutSize(java.awt.Container)

public Dimension sample code for java.awt.Dimension definition code for java.awt.Dimension  minimumLayoutSize(Container sample code for java.awt.Container definition code for java.awt.Container  target)
Returns the minimum dimensions needed to layout the visible components contained in the specified target container.

Specified by:
minimumLayoutSize sample code for java.awt.LayoutManager.minimumLayoutSize(java.awt.Container) definition code for java.awt.LayoutManager.minimumLayoutSize(java.awt.Container) in interface LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager
Parameters:
target - the container that needs to be laid out
Returns:
the minimum dimensions to lay out the subcomponents of the specified container
See Also:
preferredLayoutSize(java.awt.Container) sample code for java.awt.FlowLayout.preferredLayoutSize(java.awt.Container) definition code for java.awt.FlowLayout.preferredLayoutSize(java.awt.Container) , Container sample code for java.awt.Container definition code for java.awt.Container , Container.doLayout() sample code for java.awt.Container.doLayout() definition code for java.awt.Container.doLayout()

layoutContainer sample code for java.awt.FlowLayout.layoutContainer(java.awt.Container) definition code for java.awt.FlowLayout.layoutContainer(java.awt.Container)

public void layoutContainer(Container sample code for java.awt.Container definition code for java.awt.Container  target)
Lays out the container. This method lets each visible component take its preferred size by reshaping the components in the target container in order to satisfy the alignment of this FlowLayout object.

Specified by:
layoutContainer sample code for java.awt.LayoutManager.layoutContainer(java.awt.Container) definition code for java.awt.LayoutManager.layoutContainer(java.awt.Container) in interface LayoutManager sample code for java.awt.LayoutManager definition code for java.awt.LayoutManager
Parameters:
target - the specified component being laid out
See Also:
Container sample code for java.awt.Container definition code for java.awt.Container , Container.doLayout() sample code for java.awt.Container.doLayout() definition code for java.awt.Container.doLayout()

toString sample code for java.awt.FlowLayout.toString() definition code for java.awt.FlowLayout.toString()

public String sample code for java.lang.String definition code for java.lang.String  toString()
Returns a string representation of this FlowLayout object and its values.

Overrides:
toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a string representation of this layout