Way2Java

FlowLayout Manager Example

FlowLayout Manager Example: It is the mostly used along with panels and is the default manager for Applets and Panels.
  1. It arranges the components on the north (top) side of the container.
  2. The components, by default, centered on the north side. The alignment can be changed to left or right.
  3. The default gap between the components, either horizontally or vertically, is 5 pixels which can be changed.
  4. It is the default manager for panels and applets.
  5. This layout gives the minimum size to the component known as preferred size.

In the following program on FlowLayout Manager Example, 10 anonymous objects of buttons are created and added to the frame that is set to flow layout.

import java.awt.*;
public class FLDemo extends Frame
{
  public FLDemo()
  {
    setLayout(new FlowLayout());
                                        // create 10 anonymous button objects and add them to frame
    for(int i = 0; i < 10; i++)
    {
      add(new Button("OK " + i));
    } 
    
    setSize(300, 300);
    setVisible(true);
  }                  
  public static void main(String args[])
  {
    new FLDemo();
  }
}



setLayout(new FlowLayout());

setLayout() method is defined in Container class and inherited by Frame class. This method sets the layout to the container. Here, it is FlowLayout that is set to Frame. The above statement can be written as follows with two statements for more clarity.

FlowLayout fl = new FlowLayout()
setLayout(fl);

10 Button anonymous objects are created (as button objects are not required in the program) and added to frame set with flow layout.

add(new Button("OK " + i));

The default alignment for buttons is center (FlowLayout.CENTER) that can be changed to left or right as follows.

setLayout(new FlowLayout(FlowLayout.LEFT));
setLayout(new FlowLayout(FlowLayout.RIGHT));
setLayout(new FlowLayout(FlowLayout.CENTER));

The default gap between the components is 5 pixels both horizontally and vertically. The gaps can be changed as follows.

setLayout(new FlowLayout(FlowLayout.CENTER, 20, 40));

CENTER is the alignment, 20 is the horizontal gap (gap between the components within the row) and 40 is the vertical gap (gap between the rows).

strong> Panels are extensively used with combination of layout managers to achieve the customized arrangement of components.

Do you accept the following are possible (as it looks impossible) in Java.

  1. A frame can be created without border.
  2. It is not necessary to import java.awt.event package explicitly when java.event is already imported.
  3. It is possible to give the minimum size to the frame with all the components embedded without using setSize() method.
  4. It is possible to create a cube without having predefined drawCube() method.
  5. It is possible to draw a freelance figure on the frame with the mouse in Java.
  6. It is possible to give the size of the button as you much as you require.
  7. HeadlessException is raised when your OS does not support any AWT operation.