FileDocCategorySizeDatePackage
FlowLayout.javaAPI DocJava SE 6 API22360Tue Jun 10 00:25:14 BST 2008java.awt

FlowLayout

public class FlowLayout extends Object implements LayoutManager, 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:
  • ComponentOrientation.LEFT_TO_RIGHT
  • ComponentOrientation.RIGHT_TO_LEFT
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:
  • {@link #LEFT LEFT}
  • {@link #RIGHT RIGHT}
  • {@link #CENTER CENTER}
  • {@link #LEADING LEADING}
  • {@link #TRAILING TRAILING}

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.

version
1.58, 11/17/05
author
Arthur van Hoff
author
Sami Shaio
since
JDK1.0
see
ComponentOrientation

Fields Summary
public static final int
LEFT
This value indicates that each row of components should be left-justified.
public static final int
CENTER
This value indicates that each row of components should be centered.
public static final int
RIGHT
This value indicates that each row of components should be right-justified.
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
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.
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 LEADING TRAILING
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
int
hgap
The flow layout manager allows a seperation of components with gaps. The horizontal gap will specify the space between components and between the components and the borders of the Container.
int
vgap
The flow layout manager allows a seperation of components with gaps. The vertical gap will specify the space between rows and between the the rows and the borders of the Container.
private boolean
alignOnBaseline
If true, components will be aligned on their baseline.
private static final long
serialVersionUID
private static final int
currentSerialVersion
private int
serialVersionOnStream
This represent the currentSerialVersion which is bein used. It will be one of two values : 0 versions before Java 2 platform v1.2.. 1 versions after Java 2 platform v1.2..
Constructors Summary
public FlowLayout()
Constructs a new FlowLayout with a centered alignment and a default 5-unit horizontal and vertical gap.


                         
      
        this(CENTER, 5, 5);
    
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.

param
align the alignment value

        this(align, 5, 5);
    
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.

param
align the alignment value
param
hgap the horizontal gap between components and between the components and the borders of the Container
param
vgap the vertical gap between components and between the components and the borders of the Container

        this.hgap = hgap;
        this.vgap = vgap;
        setAlignment(align);
    
Methods Summary
public voidaddLayoutComponent(java.lang.String name, java.awt.Component comp)
Adds the specified component to the layout. Not used by this class.

param
name the name of the component
param
comp the component to be added

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

return
true if components are to be vertically aligned along their baseline
since
1.6

        return alignOnBaseline;
    
public intgetAlignment()
Gets the alignment for this layout. Possible values are FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.

return
the alignment value for this layout
see
java.awt.FlowLayout#setAlignment
since
JDK1.1

        return newAlign;
    
public intgetHgap()
Gets the horizontal gap between components and between the components and the borders of the Container

return
the horizontal gap between components and between the components and the borders of the Container
see
java.awt.FlowLayout#setHgap
since
JDK1.1

        return hgap;
    
public intgetVgap()
Gets the vertical gap between components and between the components and the borders of the Container.

return
the vertical gap between components and between the components and the borders of the Container
see
java.awt.FlowLayout#setVgap
since
JDK1.1

        return vgap;
    
public voidlayoutContainer(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.

param
target the specified component being laid out
see
Container
see
java.awt.Container#doLayout

      synchronized (target.getTreeLock()) {
        Insets insets = target.getInsets();
        int maxwidth = target.width - (insets.left + insets.right + hgap*2);
        int nmembers = target.getComponentCount();
        int x = 0, y = insets.top + vgap;
        int rowh = 0, start = 0;

        boolean ltr = target.getComponentOrientation().isLeftToRight();

        boolean useBaseline = getAlignOnBaseline();
        int[] ascent = null;
        int[] descent = null;

        if (useBaseline) {
            ascent = new int[nmembers];
            descent = new int[nmembers];
        }

        for (int i = 0 ; i < nmembers ; i++) {
            Component m = target.getComponent(i);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                m.setSize(d.width, d.height);

                if (useBaseline) {
                    int baseline = m.getBaseline(d.width, d.height);
                    if (baseline >= 0) {
                        ascent[i] = baseline;
                        descent[i] = d.height - baseline;
                    }
                    else {
                        ascent[i] = -1;
                    }
                }
                if ((x == 0) || ((x + d.width) <= maxwidth)) {
                    if (x > 0) {
                        x += hgap;
                    }
                    x += d.width;
                    rowh = Math.max(rowh, d.height);
                } else {
		    rowh = moveComponents(target, insets.left + hgap, y,
                                   maxwidth - x, rowh, start, i, ltr,
                                   useBaseline, ascent, descent);
                    x = d.width;
                    y += vgap + rowh;
                    rowh = d.height;
                    start = i;
                }
            }
        }
	moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh,
                       start, nmembers, ltr, useBaseline, ascent, descent);
      }
    
public java.awt.DimensionminimumLayoutSize(java.awt.Container target)
Returns the minimum dimensions needed to layout the visible components contained in the specified target container.

param
target the container that needs to be laid out
return
the minimum dimensions to lay out the subcomponents of the specified container
see
#preferredLayoutSize
see
java.awt.Container
see
java.awt.Container#doLayout

      synchronized (target.getTreeLock()) {
        boolean useBaseline = getAlignOnBaseline();
	Dimension dim = new Dimension(0, 0);
	int nmembers = target.getComponentCount();
        int maxAscent = 0;
        int maxDescent = 0;
        boolean firstVisibleComponent = true;

	for (int i = 0 ; i < nmembers ; i++) {
	    Component m = target.getComponent(i);
	    if (m.visible) {
		Dimension d = m.getMinimumSize();
		dim.height = Math.max(dim.height, d.height);
                if (firstVisibleComponent) {
                    firstVisibleComponent = false;
                } else {
                    dim.width += hgap;
		}
		dim.width += d.width;
                if (useBaseline) {
                    int baseline = m.getBaseline(d.width, d.height);
                    if (baseline >= 0) {
                        maxAscent = Math.max(maxAscent, baseline);
                        maxDescent = Math.max(maxDescent,
                                              dim.height - baseline);
                    }
                }
}
}

        if (useBaseline) {
            dim.height = Math.max(maxAscent + maxDescent, dim.height);
        }

	Insets insets = target.getInsets();
	dim.width += insets.left + insets.right + hgap*2;
	dim.height += insets.top + insets.bottom + vgap*2;
	return dim;





      }
    
private intmoveComponents(java.awt.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.

param
target the component which needs to be moved
param
x the x coordinate
param
y the y coordinate
param
width the width dimensions
param
height the height dimensions
param
rowStart the beginning of the row
param
rowEnd the the ending of the row
param
useBaseline Whether or not to align on baseline.
param
ascent Ascent for the components. This is only valid if useBaseline is true.
param
descent Ascent for the components. This is only valid if useBaseline is true.
return
actual row height

        switch (newAlign) {
        case LEFT:
            x += ltr ? 0 : width;
            break;
        case CENTER:
            x += width / 2;
            break;
        case RIGHT:
            x += ltr ? width : 0;
            break;
        case LEADING:
            break;
        case TRAILING:
            x += width;
            break;
        }
        int maxAscent = 0;
        int nonbaselineHeight = 0;
        int baselineOffset = 0;
        if (useBaseline) {
            int maxDescent = 0;
            for (int i = rowStart ; i < rowEnd ; i++) {
                Component m = target.getComponent(i);
                if (m.visible) {
                    if (ascent[i] >= 0) {
                        maxAscent = Math.max(maxAscent, ascent[i]);
                        maxDescent = Math.max(maxDescent, descent[i]);
                    }
                    else {
                        nonbaselineHeight = Math.max(m.getHeight(),
                                                     nonbaselineHeight);
                    }
                }
            }
            height = Math.max(maxAscent + maxDescent, nonbaselineHeight);
            baselineOffset = (height - maxAscent - maxDescent) / 2;
        }
	for (int i = rowStart ; i < rowEnd ; i++) {
            Component m = target.getComponent(i);
            if (m.isVisible()) {
                int cy;
                if (useBaseline && ascent[i] >= 0) {
                    cy = y + baselineOffset + maxAscent - ascent[i];
                }
                else {
                    cy = y + (height - m.height) / 2;
                }
                if (ltr) {
        	    m.setLocation(x, cy);
                } else {
	            m.setLocation(target.width - x - m.width, cy);
                }
                x += m.width + hgap;
	    }
	}
        return height;
    
public java.awt.DimensionpreferredLayoutSize(java.awt.Container target)
Returns the preferred dimensions for this layout given the visible components in the specified target container.

param
target the container that needs to be laid out
return
the preferred dimensions to lay out the subcomponents of the specified container
see
Container
see
#minimumLayoutSize
see
java.awt.Container#getPreferredSize

      synchronized (target.getTreeLock()) {
        Dimension dim = new Dimension(0, 0);
        int nmembers = target.getComponentCount();
        boolean firstVisibleComponent = true;
        boolean useBaseline = getAlignOnBaseline();
        int maxAscent = 0;
        int maxDescent = 0;

        for (int i = 0 ; i < nmembers ; i++) {
            Component m = target.getComponent(i);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                dim.height = Math.max(dim.height, d.height);
                if (firstVisibleComponent) {
                    firstVisibleComponent = false;
                } else {
                    dim.width += hgap;
                }
		dim.width += d.width;
                if (useBaseline) {
                    int baseline = m.getBaseline(d.width, d.height);
                    if (baseline >= 0) {
                        maxAscent = Math.max(maxAscent, baseline);
                        maxDescent = Math.max(maxDescent, d.height - baseline);
                    }
                }
	    }
	}
        if (useBaseline) {
            dim.height = Math.max(maxAscent + maxDescent, dim.height);
        }
	Insets insets = target.getInsets();
	dim.width += insets.left + insets.right + hgap*2;
	dim.height += insets.top + insets.bottom + vgap*2;
	return dim;
      }
    
private voidreadObject(java.io.ObjectInputStream 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..


                                    
       
           
    
        stream.defaultReadObject();

        if (serialVersionOnStream < 1) {
            // "newAlign" field wasn't present, so use the old "align" field.
            setAlignment(this.align);
        }
        serialVersionOnStream = currentSerialVersion;
    
public voidremoveLayoutComponent(java.awt.Component comp)
Removes the specified component from the layout. Not used by this class.

param
comp the component to remove
see
java.awt.Container#removeAll

    
public voidsetAlignOnBaseline(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.

param
alignOnBaseline whether or not components should be vertically aligned on their baseline
since
1.6

        this.alignOnBaseline = alignOnBaseline;
    
public voidsetAlignment(int align)
Sets the alignment for this layout. Possible values are
  • FlowLayout.LEFT
  • FlowLayout.RIGHT
  • FlowLayout.CENTER
  • FlowLayout.LEADING
  • FlowLayout.TRAILING

param
align one of the alignment values shown above
see
#getAlignment()
since
JDK1.1

        this.newAlign = align;

        // this.align is used only for serialization compatibility,
        // so set it to a value compatible with the 1.1 version
        // of the class

        switch (align) {
        case LEADING:
            this.align = LEFT;
            break;
        case TRAILING:
            this.align = RIGHT;
            break;
        default:
            this.align = align;
            break;
        }
    
public voidsetHgap(int hgap)
Sets the horizontal gap between components and between the components and the borders of the Container.

param
hgap the horizontal gap between components and between the components and the borders of the Container
see
java.awt.FlowLayout#getHgap
since
JDK1.1

        this.hgap = hgap;
    
public voidsetVgap(int vgap)
Sets the vertical gap between components and between the components and the borders of the Container.

param
vgap the vertical gap between components and between the components and the borders of the Container
see
java.awt.FlowLayout#getVgap
since
JDK1.1

        this.vgap = vgap;
    
public java.lang.StringtoString()
Returns a string representation of this FlowLayout object and its values.

return
a string representation of this layout

        String str = "";
        switch (align) {
          case LEFT:        str = ",align=left"; break;
          case CENTER:      str = ",align=center"; break;
          case RIGHT:       str = ",align=right"; break;
          case LEADING:     str = ",align=leading"; break;
          case TRAILING:    str = ",align=trailing"; break;
        }
        return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + str + "]";