In Java swing, Layout manager is used to position all its components, with setting properties, such as the size, the shape and the arrangement.

Following are the different types of layout managers:

  1. Flow Layout
  2. Border layout
  3. Grid Layout

Flow Layout

The FlowLayout arranges the components in a directional flow, either from left to right or from right to left. Normally all components are set to one row, according to the order of different components. If all components cannot be fit into one row, it will start a new row and fit the rest in.

import javax.swing.*; import java.awt.FlowLayout;

public class FlowLayoutExample {

public static void main(String[] args) {

// Create and set up a frame window JFrame frame = new JFrame("Layout");

// Define new buttons

JButton jb1 = new JButton("Button 1"); JButton jb2 = new JButton("Button 2"); JButton jb3 = new JButton("Button 3");

 

// Define the panel to hold the buttons JPanel panel = new JPanel();

panel.setLayout(new FlowLayout()); //setting flowlayout panel.add(jb1);

panel.add(jb2); panel.add(jb3);

// Set the window to be visible as the default to be false frame.add(panel);

frame.pack(); frame.setVisible(true);

}

}

Border Layout

A BorderLayout lays out a container, arranging its components to   fit   into   five regions: NORTH, SOUTH, EAST, WEST and CENTER. For each region, it may contain no more than one component. When adding different components, you need to specify the orientation of it to be the one of the five regions.

For BorderLayout, it can be constructed like below:

  • BorderLayout(): construct a border layout with no gaps between components.
  • BorderLayout(int hgap, int vgap): construct a border layout with specified gaps between components.

// Define new buttons with different regions

JButton jb1 = new JButton("NORTH");

JButton jb2 = new JButton("SOUTH");

JButton jb3 = new JButton("WEST");

JButton jb4 = new JButton("EAST");

JButton jb5 = new JButton("CENTER");

// Define the panel to hold the buttons

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.add(jb1, BorderLayout.NORTH);

panel.add(jb2, BorderLayout.SOUTH);

panel.add(jb3, BorderLayout.WEST);

panel.add(jb4, BorderLayout.EAST);

panel.add(jb5, BorderLayout.CENTER);

               

Grid Layout

The GridLayout manager is used to lay out the components in a rectangle grid, which has been divided into equal-sized rectangles and one component is placed in each rectangle. It can constructed with following methods:

  • GridLayout(): construct a grid layout with one column per component in a single row.
  • GridLayout(int row, int col): construct a grid layout with specified numbers of rows and columns.
  • GridLayout(int row, int col, int hgap, int vgap): construct a grid layout with specified rows, columns and gaps between components.

// Define new buttons

JButton jb1 = new JButton("Button 1");

JButton jb2 = new JButton("Button 2");

JButton jb3 = new JButton("Button 3");

JButton jb4 = new JButton("Button 4");

JButton jb5 = new JButton("Button 5");

 

// Define the panel to hold the buttons

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(3, 2));

panel.add(jb1);

panel.add(jb2);

panel.add(jb3);

panel.add(jb4);

panel.add(jb5);