java.awt.FlowLayout is the default Layout of Applet, JApplet, Panel, JPanel. Characteristics of FlowLayout is to attach the components one after another in specified direction. Main advantage of using FlowLayout is that: It do not resize the component. It just allocate the space required by the component. to set any layout of any component the method which can be used is: componetObject.setLayout(layoutManagerObject);

FlowLayout Constructors

  1. FlowLayout()

    This constructs is used to create a center aligned and a default 5-unit horizontal and vertical gap based FlowLayout object.

  2. FlowLayout(int align)

    This constructs is used to create the specified alignment and a default 5-unit horizontal and vertical gap based FlowLayout object.

  3. FlowLayout(int align, int hgap, int vgap)

    This constructor is used to create a new flow layout with the indicated alignment and the indicated horizontal and vertical gaps.


As discussed in the above constructor summary, We have a constructor which takes alignment value as integer. Here the problem is: We can not remember exact value for each alignment. For this , Java provides one easy option in which we have to pass a static VARIABLE as a argument. Again that static value will get from the same class we are using. For example, Here in our case we would like to pass an integer value which creates a LEFT aligned layout for us. For that we can use a public final and static int variable of FlowLayout class which is named as LEFT. And as you know: To access a static variable/method we don’t need object. That can be done by simply writing class name. i.e. FlowLayout.LEFT. So the syntax to change the layout of our base class will be this.setLayout(new FlowLayout()); which will create a CENTER aligned FlowLayout. For left Alignment we can use this.setLayout(new FlowLayout(FlowLayout.LEFT));

FlowLayout Alignments

  1. CENTER

    This creates a center aligned FlowLayout. Components will be aligned to center.

  2. LEFT

    This creates a left aligned FlowLayout. Components will be aligned to left.

  3. RIGHT

    This creates a right aligned FlowLayout. Components will be aligned to right.

  4. LEADING

    This aligns all the components to the LEADING edge of parent/container component. In case the parent component is Right aligned and we use LEADING, the alignment which will be generated will be Right.

  5. TRAILING

    This aligns all the components to the TRAILING edge of parent/container component. In case the parent component is Right aligned and we use TRAILING, the alignment which will be generated will be LEFT.


FlowLayout with Center Alignment

Program:

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

public class FlowLayoutDemo extends Applet
{
	Button b1, b2, b3;
	public void init()
	{
		FlowLayout fl = new FlowLayout();
		setLayout(fl);
		
		b1 = new Button("Button1");
		b2 = new Button("Button2");
		b3 = new Button("Button3");
		
		add(b1);
		add(b2);
		add(b3);
	}	
}
/*
<applet code="FlowLayoutDemo" width="500" height="200"></applet>
*/

Output:


FlowLayout with LEFT Alignment

Program:

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

public class FlowLayoutLeftDemo extends Applet
{
	Button b1, b2, b3;
	public void init()
	{
		FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
		setLayout(fl);
		
		b1 = new Button("Button1");
		b2 = new Button("Button2");
		b3 = new Button("Button3");
		
		add(b1);
		add(b2);
		add(b3);
	}	
}
/*
<applet code="FlowLayoutLeftDemo" width="500" height="200"></applet>
*/

Output: