APIdia Launch Partner
Top Description Fields Constructors Methods
java.awt

public Class FlowLayout

extends Object
implements LayoutManager, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.awt.LayoutManager
Imports
java.io.IOException, .ObjectInputStream, .Serial

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 a window 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 program:

import java.awt.Button; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Frame; public class FlowLayoutExample { public static void main(String[] args) throws Exception { EventQueue.invokeAndWait(() -> { Frame frame = new Frame("FlowLayout"); frame.setLayout(new FlowLayout()); frame.add(new Button("OK")); frame.add(new Button("Open")); frame.add(new Button("Close")); frame.pack(); frame.setVisible(true); }); } }
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;

public class FlowLayoutExample {

    public static void main(String[] args) throws Exception {
        EventQueue.invokeAndWait(() -> {
            Frame frame = new Frame("FlowLayout");
            frame.setLayout(new FlowLayout());
            frame.add(new Button("OK"));
            frame.add(new Button("Open"));
            frame.add(new Button("Close"));
            frame.pack();
            frame.setVisible(true);
        });
    }
}

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

Authors
Arthur van Hoff, Sami Shaio
Since
1.0
See Also
ComponentOrientation

Field Summary

Modifier and TypeField and Description
pack-priv int
align

align is the property that determines how each row distributes empty space.

private boolean
public static final int
CENTER

This value indicates that each row of components should be centered.

private static final int
pack-priv int
hgap

The flow layout manager allows a separation of components with gaps.

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.

public static final int
LEFT

This value indicates that each row of components should be left-justified.

pack-priv int
newAlign

newAlign is the property that determines how each row distributes empty space for the Java 2 platform, v1.2 and greater.

public static final int
RIGHT

This value indicates that each row of components should be right-justified.

private int
serialVersionOnStream

This represents the currentSerialVersion which is being used.

private static final long
serialVersionUID

Use serialVersionUID from JDK 1.1 for interoperability.

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.

pack-priv int
vgap

The flow layout manager allows a separation of components with gaps.

Constructor Summary

AccessConstructor and Description
public
FlowLayout()

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

public
FlowLayout(int
the alignment value
align
)

Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap.

public
FlowLayout(int
the alignment value
align
,
int
the horizontal gap between components and between the components and the borders of the Container
hgap
,
int
the vertical gap between components and between the components and the borders of the Container
vgap
)

Creates a new flow layout manager with the indicated alignment and the indicated horizontal and vertical gaps.

Method Summary

Modifier and TypeMethod and Description
public void
addLayoutComponent(String
the name of the component
name
,
Component
the component to be added
comp
)

Implements java.awt.LayoutManager.addLayoutComponent.

Adds the specified component to the layout.

public int

Returns:

the alignment value for this layout
getAlignment
()

Gets the alignment for this layout.

public boolean

Returns:

true if components are to be vertically aligned along their baseline
getAlignOnBaseline
()

Returns true if components are to be vertically aligned along their baseline.

public int

Returns:

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

Gets the horizontal gap between components and between the components and the borders of the Container

public int

Returns:

the vertical gap between components and between the components and the borders of the Container
getVgap
()

Gets the vertical gap between components and between the components and the borders of the Container.

public void
layoutContainer(Container
the specified component being laid out
target
)

Implements java.awt.LayoutManager.layoutContainer.

Lays out the container.

public Dimension

Returns:

the minimum dimensions to lay out the subcomponents of the specified container
minimumLayoutSize
(Container
the container that needs to be laid out
target
)

Implements java.awt.LayoutManager.minimumLayoutSize.

Returns the minimum dimensions needed to layout the visible components contained in the specified target container.

private int

Returns:

actual row height
moveComponents
(Container
the component which needs to be moved
target
,
int
the x coordinate
x
,
int
the y coordinate
y
,
int
the width dimensions
width
,
int
the height dimensions
height
,
int
the beginning of the row
rowStart
,
int
the ending of the row
rowEnd
,
boolean ltr, boolean
Whether or not to align on baseline.
useBaseline
,
int[]
Ascent for the components. This is only valid if useBaseline is true.
ascent
,
int[]
Ascent for the components. This is only valid if useBaseline is true.
descent
)

Centers the elements in the specified row, if there is any slack.

public Dimension

Returns:

the preferred dimensions to lay out the subcomponents of the specified container
preferredLayoutSize
(Container
the container that needs to be laid out
target
)

Implements java.awt.LayoutManager.preferredLayoutSize.

Returns the preferred dimensions for this layout given the visible components in the specified target container.

private void
readObject(ObjectInputStream
the ObjectInputStream to read
stream
)

Reads this object out of a serialization stream, handling objects written by older versions of the class that didn't contain all of the fields we use now.

public void
removeLayoutComponent(Component
the component to remove
comp
)

Implements java.awt.LayoutManager.removeLayoutComponent.

Removes the specified component from the layout.

public void
setAlignment(int
one of the alignment values shown above
align
)

Sets the alignment for this layout.

public void
setAlignOnBaseline(boolean
whether or not components should be vertically aligned on their baseline
alignOnBaseline
)

Sets whether or not components should be vertically aligned along their baseline.

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

Sets the horizontal gap between components and between the components and the borders of the Container.

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

Sets the vertical gap between components and between the components and the borders of the Container.

public String

Returns:

a string representation of this layout
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this FlowLayout object and its values.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

alignback to summary
pack-priv int align

align is the property that determines how each row distributes empty space. It can be one of the following values:

  • LEFT
  • RIGHT
  • CENTER
See Also
getAlignment, setAlignment
alignOnBaselineback to summary
private boolean alignOnBaseline
CENTERback to summary
public static final int CENTER

This value indicates that each row of components should be centered.

currentSerialVersionback to summary
private static final int currentSerialVersion
hgapback to summary
pack-priv int hgap

The flow layout manager allows a separation of components with gaps. The horizontal gap will specify the space between components and between the components and the borders of the Container.

See Also
getHgap(), setHgap(int)
LEADINGback to summary
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
See Also
java.awt.Component#getComponentOrientation, java.awt.ComponentOrientation
LEFTback to summary
public static final int LEFT

This value indicates that each row of components should be left-justified.

newAlignback to summary
pack-priv int newAlign

newAlign is the property that determines how each row distributes empty space for the Java 2 platform, v1.2 and greater. It can be one of the following three values:

  • LEFT
  • RIGHT
  • CENTER
  • LEADING
  • TRAILING
Since
1.2
See Also
getAlignment, setAlignment
serialVersionOnStreamback to summary
private int serialVersionOnStream

This represents the currentSerialVersion which is being used. It will be one of two values: 0 versions before Java 2 platform v1.2, 1 versions after Java 2 platform v1.2.

Since
1.2
serialVersionUIDback to summary
private static final long serialVersionUID

Use serialVersionUID from JDK 1.1 for interoperability.

Annotations
@Serial
TRAILINGback to summary
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
See Also
java.awt.Component#getComponentOrientation, java.awt.ComponentOrientation
vgapback to summary
pack-priv int vgap

The flow layout manager allows a separation of components with gaps. The vertical gap will specify the space between rows and between the the rows and the borders of the Container.

See Also
getHgap(), setHgap(int)

Constructor Detail

FlowLayoutback to summary
public FlowLayout()

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

FlowLayoutback to summary
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:int

the alignment value

FlowLayoutback to summary
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:int

the alignment value

hgap:int

the horizontal gap between components and between the components and the borders of the Container

vgap:int

the vertical gap between components and between the components and the borders of the Container

Method Detail

addLayoutComponentback to summary
public void addLayoutComponent(String name, Component comp)

Implements java.awt.LayoutManager.addLayoutComponent.

Adds the specified component to the layout. Not used by this class.

Parameters
name:String

the name of the component

comp:Component

the component to be added

getAlignmentback to summary
public int getAlignment()

Gets the alignment for this layout. Possible values are FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.

Returns:int

the alignment value for this layout

Since
1.1
See Also
java.awt.FlowLayout#setAlignment
getAlignOnBaselineback to summary
public boolean getAlignOnBaseline()

Returns true if components are to be vertically aligned along their baseline. The default is false.

Returns:boolean

true if components are to be vertically aligned along their baseline

Since
1.6
getHgapback to summary
public int getHgap()

Gets the horizontal gap between components and between the components and the borders of the Container

Returns:int

the horizontal gap between components and between the components and the borders of the Container

Since
1.1
See Also
java.awt.FlowLayout#setHgap
getVgapback to summary
public int getVgap()

Gets the vertical gap between components and between the components and the borders of the Container.

Returns:int

the vertical gap between components and between the components and the borders of the Container

Since
1.1
See Also
java.awt.FlowLayout#setVgap
layoutContainerback to summary
public void layoutContainer(Container target)

Implements java.awt.LayoutManager.layoutContainer.

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.

Parameters
target:Container

the specified component being laid out

See Also
Container, java.awt.Container#doLayout
minimumLayoutSizeback to summary
public Dimension minimumLayoutSize(Container target)

Implements java.awt.LayoutManager.minimumLayoutSize.

Returns the minimum dimensions needed to layout the visible components contained in the specified target container.

Parameters
target:Container

the container that needs to be laid out

Returns:Dimension

the minimum dimensions to lay out the subcomponents of the specified container

See Also
preferredLayoutSize, java.awt.Container, java.awt.Container#doLayout
moveComponentsback to summary
private int moveComponents(Container target, int x, int y, int width, int height, int rowStart, int rowEnd, boolean ltr, boolean useBaseline, int[] ascent, int[] descent)

Centers the elements in the specified row, if there is any slack.

Parameters
target:Container

the component which needs to be moved

x:int

the x coordinate

y:int

the y coordinate

width:int

the width dimensions

height:int

the height dimensions

rowStart:int

the beginning of the row

rowEnd:int

the ending of the row

useBaseline:boolean

Whether or not to align on baseline.

ascent:int[]

Ascent for the components. This is only valid if useBaseline is true.

descent:int[]

Ascent for the components. This is only valid if useBaseline is true.

Returns:int

actual row height

preferredLayoutSizeback to summary
public Dimension preferredLayoutSize(Container target)

Implements java.awt.LayoutManager.preferredLayoutSize.

Returns the preferred dimensions for this layout given the visible components in the specified target container.

Parameters
target:Container

the container that needs to be laid out

Returns:Dimension

the preferred dimensions to lay out the subcomponents of the specified container

See Also
Container, minimumLayoutSize, java.awt.Container#getPreferredSize
readObjectback to summary
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException

Reads this object out of a serialization stream, handling objects written by older versions of the class that didn't contain all of the fields we use now.

Parameters
stream:ObjectInputStream

the ObjectInputStream to read

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
ClassNotFoundException:
if the class of a serialized object could not be found
removeLayoutComponentback to summary
public void removeLayoutComponent(Component comp)

Implements java.awt.LayoutManager.removeLayoutComponent.

Removes the specified component from the layout. Not used by this class.

Parameters
comp:Component

the component to remove

See Also
java.awt.Container#removeAll
setAlignmentback to summary
public void setAlignment(int align)

Sets the alignment for this layout. Possible values are

  • FlowLayout.LEFT
  • FlowLayout.RIGHT
  • FlowLayout.CENTER
  • FlowLayout.LEADING
  • FlowLayout.TRAILING
Parameters
align:int

one of the alignment values shown above

Since
1.1
See Also
getAlignment()
setAlignOnBaselineback to summary
public void setAlignOnBaseline(boolean alignOnBaseline)

Sets whether or not components should be vertically aligned along their baseline. Components that do not have a baseline will be centered. The default is false.

Parameters
alignOnBaseline:boolean

whether or not components should be vertically aligned on their baseline

Since
1.6
setHgapback to summary
public void setHgap(int hgap)

Sets the horizontal gap between components and between the components and the borders of the Container.

Parameters
hgap:int

the horizontal gap between components and between the components and the borders of the Container

Since
1.1
See Also
java.awt.FlowLayout#getHgap
setVgapback to summary
public void setVgap(int vgap)

Sets the vertical gap between components and between the components and the borders of the Container.

Parameters
vgap:int

the vertical gap between components and between the components and the borders of the Container

Since
1.1
See Also
java.awt.FlowLayout#getVgap
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this FlowLayout object and its values.

Returns:String

a string representation of this layout