Flow
Home Up Border Card Flow Grid Null

 

Flow Layout Manager

The FlowLayout manager is the default layout manager for any user interface component.   There are three ways to specify the FlowLayout manager:

  1. setLayout (new FlowLayout());

    In this case, components are added to the manager from left to right and from top to bottom.  Within a row, they are centered.
  2. setLayout (new FlowLayout(int));

    In this case, you must specify the alignment.  The legal values are CENTER, LEFT, and RIGHT.  CENTER indicates that components are centered within a row (the same as case 1), LEFT indicates that components are left justified within a row, and RIGHT indicates that components are right justified within a row.
  3. setLayout (new FlowLayout(int, int, int));

    In this case, you specify the alignment (CENTER, LEFT, or RIGHT), the horizontal gap in pixels between components, and the vertical gap in pixels between components.

To display a component on the window, call the method "add".

Example

In the code below, five buttons (with labels "1", "2", "3", "4", and "5") are created and displayed on the window with the FlowLayout manager.   Run this applet and experiment with the size of the drawing area to better understand how the flow manager works.  Remember that this code should be stored in a file called Flow.java!

import java.awt.*;
import java.applet.*;            
public class Flow extends Applet
{
	public void init ()
	{
		setLayout(new FlowLayout());            
		add ( new Button("1") );
		add ( new Button("2") );
		add ( new Button("3") );
		add ( new Button("4") );
		add ( new Button("5") );
	}
		            
}         
What You'll See: