All About Class FlowLayout in AWT in Java

0
189
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

Class FlowLayout in AWT in Java

FlowLayourt implements the LayoutManager interface to layout Component objects in a Container. The most basic and default layout in java is FlowLayout.

The manager implements a simple Layout style which is similar to how words flow in a text editor. Components are laid from the upper left corner, left to right and top to bottom. When no more components fit on a line, the next one will appear in the next line. It fits as many components as it can in a row before moving onto the next row.

The constructor allows us to specify one of the three constants as an alignment value for the rows and also allows us to specify the horizontal spacing between components and vertical spacing between the rows.

A small space is left between components, above and below. However, the user can change this gap, alignment, etc. The default gap is five pixels and the default alignment is center. Below are the different functions for FlowLayout.FlowLayout is typically used to arrange buttons in a panel.

Any application should never call the LayoutManager methods of this class directly. The Container for which the FlowLayout is registered does all on its own.

The class structure of class FlowLayout is given as:

public class java.awt.FlowLayout extends java.lang.Object implements java.awt.LayoutManager,java.io.Serializable{
//member elements
public final static int CENTER;
//this indicates that each row of components should be at center.
public final static int LEFT;
//this indicates that each row of components should be at left justified.
public final static int RIGHT;
//this indicates that each row of components should be at right justified.
//constructor
public FlowLayout();
public FlowLayout(int alignment);//alignment can be LEFT,CENTER,RIGHT
public FlowLayout(int alignment,int hgap,int vgap));//alignment can be LEFT,CENTER,RIGHT
//hgap and vgap are the space between two components horizontally and vertically.
//methods
public void addLayoutComponent(String name,Component comp);//From LayoutManager
public void layoutContainer(Container target);//From LayoutManager
public int getAlignment();
public int getHgap();
public int getVgap();
public Dimension minimumLayoutSize(Container target)//From LayoutManager
public Dimension preferredLayoutSize(Container target)//From LayoutManager
public void removeLayoutComponent(Component comp);
public void setAlignment(int allignment);
public void setHgap(int hgap);
public void setVgap(int vgap);
public String toString();
}
 

public FlowLayout();
Creates a new FlowLayout manager with a centered alignment and a default 5 pixel horizontal and vertical gap.
public FlowLayout(int alignment);
Creates a new FlowLayout manager with a centered alignment and a default 5 pixel horizontal and vertical gap. The alignment argument must be FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER

an example:


component.setLayout(new FlowLayout());
for(int i=0;i<20;i++);
component.add(New Button("OK"));

Components will be compacted to their smallest size in a FlowLayout.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here