What is a LayoutManager and types of LayoutManager in Java?


The Layout managers enable us to control the way in which visual components are arranged in the GUI forms by determining the size and position of components within the containers.

Types of LayoutManager

There are 6 layout managers in Java

  • FlowLayout: It arranges the components in a container like the words on a page. It fills the top line from left to right and top to bottom. The components are arranged in the order as they are added i.e. first components appears at top left, if the container is not wide enough to display all the components, it is wrapped around the line. Vertical and horizontal gap between components can be controlled. The components can be left, center or right aligned.
  • BorderLayout: It arranges all the components along the edges or the middle of the container i.e. top, bottom, right and left edges of the area. The components added to the top or bottom gets its preferred height, but its width will be the width of the container and also the components added to the left or right gets its preferred width, but its height will be the remaining height of the container. The components added to the center gets neither its preferred height or width. It covers the remaining area of the container.
  • GridLayout: It arranges all the components in a grid of equally sized cells, adding them from the left to right and top to bottom. Only one component can be placed in a cell and each region of the grid will have the same size. When the container is resized, all cells are automatically resized. The order of placing the components in a cell is determined as they were added.
  • GridBagLayout: It is a powerful layout which arranges all the components in a grid of cells and maintains the aspect ration of the object whenever the container is resized. In this layout, cells may be different in size. It assigns a consistent horizontal and vertical gap among components. It allows us to specify a default alignment for components within the columns or rows.
  • BoxLayout: It arranges multiple components in either vertically or horizontally, but not both. The components are arranged from left to right or top to bottom. If the components are aligned horizontally, the height of all components will be the same and equal to the largest sized components. If the components are aligned vertically, the width of all components will be the same and equal to the largest width components.
  • CardLayout: It arranges two or more components having the same size. The components are arranged in a deck, where all the cards of the same size and the only top card are visible at any time. The first component added in the container will be kept at the top of the deck. The default gap at the left, right, top and bottom edges are zero and the card components are displayed either horizontally or vertically.

Example

import java.awt.*;
import javax.swing.*;
public class LayoutManagerTest extends JFrame {
   JPanel flowLayoutPanel1, flowLayoutPanel2, gridLayoutPanel1, gridLayoutPanel2, gridLayoutPanel3;
   JButton one, two, three, four, five, six;
   JLabel bottom, lbl1, lbl2, lbl3;
   public LayoutManagerTest() {
      setTitle("LayoutManager Test");
      setLayout(new BorderLayout()); // Set BorderLayout for JFrame
      flowLayoutPanel1 = new JPanel();
      one = new JButton("One");
      two = new JButton("Two");
      three = new JButton("Three");
      flowLayoutPanel1.setLayout(new FlowLayout(FlowLayout.CENTER)); // Set FlowLayout Manager
      flowLayoutPanel1.add(one);
      flowLayoutPanel1.add(two);
      flowLayoutPanel1.add(three);
      flowLayoutPanel2 = new JPanel();
      bottom = new JLabel("This is South");
      flowLayoutPanel2.setLayout (new FlowLayout(FlowLayout.CENTER)); // Set FlowLayout Manager
      flowLayoutPanel2.add(bottom);
      gridLayoutPanel1 = new JPanel();
      gridLayoutPanel2 = new JPanel();
      gridLayoutPanel3 = new JPanel();
      lbl1 = new JLabel("One");
      lbl2 = new JLabel("Two");
      lbl3 = new JLabel("Three");
      four = new JButton("Four");
      five = new JButton("Five");
      six = new JButton("Six");
      gridLayoutPanel2.setLayout(new GridLayout(1, 3, 5, 5)); // Set GridLayout Manager
      gridLayoutPanel2.add(lbl1);
      gridLayoutPanel2.add(lbl2);
      gridLayoutPanel2.add(lbl3);
      gridLayoutPanel3.setLayout(new GridLayout(3, 1, 5, 5)); // Set GridLayout Manager
      gridLayoutPanel3.add(four);
      gridLayoutPanel3.add(five);
      gridLayoutPanel3.add(six);
      gridLayoutPanel1.setLayout(new GridLayout(2, 1)); // Set GridLayout Manager
      gridLayoutPanel1.add(gridLayoutPanel2);
      gridLayoutPanel1.add(gridLayoutPanel3);
      add(flowLayoutPanel1, BorderLayout.NORTH);
      add(flowLayoutPanel2, BorderLayout.SOUTH);
      add(gridLayoutPanel1, BorderLayout.CENTER);
      setSize(400, 325);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String args[]) {
      new LayoutManagerTest();
   }
}

Output

Start learning Java with our Java tutorial from scratch.

raja
raja

e

Updated on: 19-Feb-2024

28K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements