Layout Managers in Java: Part 1

Introduction 

 
In this article, we will learn about layout managers used in Java, with simple examples.
 

Layout Manager in Java

 
A layout manager is an object that controls the size and position of the components in the container. Every container object has a layout manager object that controls its layout.
 
Actually, layout managers are used to arrange the components in a specific manner.  It is an interface that is implemented by all the classes of layout managers.  There are some classes that represent the layout managers.
 
The Abstract Windowing Toolkit (AWT) has the following five layout managers:
  • java.awt.BorderLayout
  • java.awt.FlowLayout
  • java.awt.GridLayout
  • java.awt.CardLayout
  • java.awt.GridBagLayout
Some of these are used in the swing:
  • javax.swing.BoxLayout
  • javax.swing.ScrollPanelLayout
  • javax.swing.GroupLayout
  • javax.swing.SpringLayout
  • and so on
In this part of the article, we will discuss the top three layout managers.
 

General rules for using layout managers

 
Default managers
  • For “JPanel” objects, the default layout manager is an instance of the “FlowLayout” class.
  • For “JFrame” object, the default layout manager is an instance of the “BorderLayout” class.
This layout manager is automatically consulted each time the container may need to change its interface.
 
How to create a layout manager and associate it with a container:
  • To use the default layout manager, there is nothing to do. The constructor for each container creates a layout manager instance and initializes the container to use it.
  • To use a non-default layout manager, create an instance of the desired layout manager class and indicate the container, to use it.
The following are the methods that result in calls to the container's layout manager:
  • add(), remove(), removeAll(): They add and remove Components from a Container; you can call them at any time.
     
  • doLayout(): It is called as the result of any painting request to a Container. It requests that the Container place and size itself and the components it contains.
     
  • getPreferredSize(), getminimum size(), getmaximum size(): They return the Container's original size, minimum size and maximum size, respectively. The values returned are just hints; they have no effect unless, your program enforces these sizes.
      

Border Layout Manager

 
In the Border Layout Manager, the components are positioned in five different areas (regions).   In other words, North, South, East, West and Center.  Each region may contain only one component.
 
If you enlarge the window, you will notice that the center area gets as much of the newly available space, as possible. The other area will expand, only as much as necessary, to keep the available space filled.
 
Example
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. public class Border extends JFrame  
  5. implements ActionListener {  
  6.  private JButton b[];  
  7.  private String names[] = {  
  8.   "Hide North Border",  
  9.   "Hide South Border",  
  10.   "Hide East Border",  
  11.   "Hide West Border",  
  12.   "Hide Center Border"  
  13.  };  
  14.  private BorderLayout layout;  
  15.  public Border() {  
  16.   super("BorderLayout");  
  17.   layout = new BorderLayout(55);  
  18.   Container c = getContentPane();  
  19.   c.setLayout(layout);  
  20.   b = new JButton[names.length];  
  21.   for (int i = 0; i < names.length; i++) {  
  22.    b[i] = new JButton(names[i]);  
  23.    b[i].addActionListener(this);  
  24.   }  
  25.   c.add(b[0], BorderLayout.NORTH);  
  26.   c.add(b[1], BorderLayout.SOUTH);  
  27.   c.add(b[2], BorderLayout.EAST);  
  28.   c.add(b[3], BorderLayout.WEST);  
  29.   c.add(b[4], BorderLayout.CENTER);  
  30.   setSize(400300);  
  31.   show();  
  32.  }  
  33.  public void actionPerformed(ActionEvent e) {  
  34.   for (int i = 0; i < b.length; i++)  
  35.    if (e.getSource() == b[i])  
  36.     b[i].setVisible(false);  
  37.    else  
  38.     b[i].setVisible(true);  
  39.   layout.layoutContainer(getContentPane());  
  40.  }  
  41.  public static void main(String args[]) {  
  42.   Border bord = new Border();  
  43.   bord.addWindowListener(new WindowAdapter() {  
  44.    public void windowClosing(WindowEvent e) {  
  45.     System.exit(0);  
  46.    }  
  47.   });  
  48.  }  
  49. }   
Output
 
Border layout manager 
 

Grid Layout Manager

 
Grid Layout is used to place the components in a grid of cells (rectangular).  Each component takes the available space within its cell.  Each cell, has exactly the same size and displays only one component.  If the Grid Layout window is expanded, the Grid Layout changes the cell size, so that the cells are as large as possible.
 
In other words, the layout manager divides the container into a grid, so that components can be placed in rows and columns.  Each component will have the same width and height.  The components are added to the grid starting at the top-left cell and proceeding left-to-right, until the row is full.  Then go to the next row.  This type of layout is known as, the Grid Layout Manager.
 
Example
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. public class Grid extends JFrame implements ActionListener {  
  5.  private JButton b[];  
  6.  private String names[] = {  
  7.   "Contacts",  
  8.   "Message",  
  9.   "Call Log",  
  10.   "Games",  
  11.   "Settings",  
  12.   "Applications",  
  13.   "Music",  
  14.   "Gallery",  
  15.   "Organiser"  
  16.  };  
  17.  private boolean toggle = true;  
  18.  private Container c;  
  19.  private GridLayout grid1, grid2, grid3;  
  20.  public Grid() {  
  21.   super("GridLayout");  
  22.   grid1 = new GridLayout(2355);  
  23.   grid2 = new GridLayout(32);  
  24.   grid3 = new GridLayout(35);  
  25.   c = getContentPane();  
  26.   c.setLayout(grid3);  
  27.   b = new JButton[names.length];  
  28.   for (int i = 0; i < names.length; i++) {  
  29.    b[i] = new JButton(names[i]);  
  30.    b[i].addActionListener(this);  
  31.    c.add(b[i]);  
  32.   }  
  33.   setSize(400400);  
  34.   show();  
  35.  }  
  36.  public void actionPerformed(ActionEvent e) {  
  37.   if (toggle)  
  38.    c.setLayout(grid3);  
  39.   else if (toggle)  
  40.    c.setLayout(grid2);  
  41.   else  
  42.    c.setLayout(grid1);  
  43.   toggle = !toggle;  
  44.   c.validate();  
  45.  }  
  46.  public static void main(String args[]) {  
  47.   Grid G = new Grid();  
  48.   G.addWindowListener(new WindowAdapter() {  
  49.    public void windowClosing(WindowEvent e) {  
  50.     System.exit(0);  
  51.    }  
  52.   });  
  53.  }  
  54. }   
Output
 
Grid Layout manager 
 

Flow Layout manager

 
The flow layout, is the most basic layout manager in which components are placed from left to right, as they were added.  When the horizontal row is too small, to put all the components in one row, then it uses multiple rows.  You can align the components left, right, or center (default).
 
Example
  1. import java.awt.*;  
  2. import javax.swing.*;  
  3. public class Flow {  
  4.  JFrame f;  
  5.  public Flow() {  
  6.   f = new JFrame();  
  7.   JButton b1 = new JButton("Red");  
  8.   JButton b2 = new JButton("Green");  
  9.   JButton b3 = new JButton("Yellow");  
  10.   JButton b4 = new JButton("Purple");  
  11.   JButton b5 = new JButton("Blue");  
  12.   JButton b6 = new JButton("Pink");  
  13.   JButton b7 = new JButton("Brown");  
  14.   f.add(b1);  
  15.   f.add(b2);  
  16.   f.add(b3);  
  17.   f.add(b4);  
  18.   f.add(b5);  
  19.   f.add(b6);  
  20.   f.add(b7);  
  21.   f.setLayout(new FlowLayout(FlowLayout.LEFT));  
  22.   f.setSize(400200);  
  23.   f.setVisible(true);  
  24.  }  
  25.  public static void main(String[] args) {  
  26.   new Flow();  
  27.  }  
  28. }   
Output
 
Flow Layout manager 
 
For other layout managers, please refer the link given below:


Similar Articles