by Dinesh Thakur

The FlowLayout is one the most popular and simplest layout manager. It places components in a container from left to right in the order in which they were added to the container. When one row is filled, layout advances to the next row. It is analogous to lines of text in a paragraph. Components managed by a FlowLayout are always set to their preferred size (both width and height) regardless of the size of the parent container. Whenever the container is resized then the components in the container are also adjusted from the top-left comer. The FlowLayout is the default layout manager for a Panel, Applet and JPanel.

The java.awt.FlowLayout class contain the following constructors and methods:

Method

Description

FlowLayout ()

Creates a default FlowLayout manager: centered alignment, horizontal spacing and 5 vertical units.

FlowLayout (int)

Creates a FlowLayout manager with alignment and given horizontal and vertical spacing of 5 units.

FlowLayout (int, int, int)

Creates a FlowLayout manager with alignment, horizontal and vertical spacing data.

getAlignment ()

Gets the alignment of this layout.

getHgap ()

Gets the horizontal spacing.

getVgap ()

Gets the vertical spacing.

setAlignment (int)

Specifies the alignment of this layout.

setHgap (int)

Specifies the horizontal spacing.

setVgap (int)

Specifies the vertical spacing.

 

Also has the constant-FlowLayout.LEFT, and FlowLayout.CENTER FlowLayout.RIGHT who can be used to specify or determine the alignment of this layout manager.

import java.awt.*;

class FlowLayoutExample extends Frame
{
     FlowLayoutExample()
     {
         Button[] button =new Button[10];
         setLayout(new FlowLayout());
         for(int i=0;i<button.length;i++)
             {
                  button[i]=new Button("Button "+(i+1));
                  add(button[i]);
             }
       }
}
  class FlowLayoutJavaExample
  {
       public static void main(String args[])
      {
             FlowLayoutExample frame = new FlowLayoutExample();
             frame.setTitle("FlowLayout in Java Example");
             frame.setSize(400,150);
             frame.setVisible(true);
       }
  }

FlowLayout in Java Example