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
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Border extends JFrame
- implements ActionListener {
- private JButton b[];
- private String names[] = {
- "Hide North Border",
- "Hide South Border",
- "Hide East Border",
- "Hide West Border",
- "Hide Center Border"
- };
- private BorderLayout layout;
- public Border() {
- super("BorderLayout");
- layout = new BorderLayout(5, 5);
- Container c = getContentPane();
- c.setLayout(layout);
- b = new JButton[names.length];
- for (int i = 0; i < names.length; i++) {
- b[i] = new JButton(names[i]);
- b[i].addActionListener(this);
- }
- c.add(b[0], BorderLayout.NORTH);
- c.add(b[1], BorderLayout.SOUTH);
- c.add(b[2], BorderLayout.EAST);
- c.add(b[3], BorderLayout.WEST);
- c.add(b[4], BorderLayout.CENTER);
- setSize(400, 300);
- show();
- }
- public void actionPerformed(ActionEvent e) {
- for (int i = 0; i < b.length; i++)
- if (e.getSource() == b[i])
- b[i].setVisible(false);
- else
- b[i].setVisible(true);
- layout.layoutContainer(getContentPane());
- }
- public static void main(String args[]) {
- Border bord = new Border();
- bord.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- }
- }
Output
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
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Grid extends JFrame implements ActionListener {
- private JButton b[];
- private String names[] = {
- "Contacts",
- "Message",
- "Call Log",
- "Games",
- "Settings",
- "Applications",
- "Music",
- "Gallery",
- "Organiser"
- };
- private boolean toggle = true;
- private Container c;
- private GridLayout grid1, grid2, grid3;
- public Grid() {
- super("GridLayout");
- grid1 = new GridLayout(2, 3, 5, 5);
- grid2 = new GridLayout(3, 2);
- grid3 = new GridLayout(3, 5);
- c = getContentPane();
- c.setLayout(grid3);
- b = new JButton[names.length];
- for (int i = 0; i < names.length; i++) {
- b[i] = new JButton(names[i]);
- b[i].addActionListener(this);
- c.add(b[i]);
- }
- setSize(400, 400);
- show();
- }
- public void actionPerformed(ActionEvent e) {
- if (toggle)
- c.setLayout(grid3);
- else if (toggle)
- c.setLayout(grid2);
- else
- c.setLayout(grid1);
- toggle = !toggle;
- c.validate();
- }
- public static void main(String args[]) {
- Grid G = new Grid();
- G.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- }
- }
Output
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
- import java.awt.*;
- import javax.swing.*;
- public class Flow {
- JFrame f;
- public Flow() {
- f = new JFrame();
- JButton b1 = new JButton("Red");
- JButton b2 = new JButton("Green");
- JButton b3 = new JButton("Yellow");
- JButton b4 = new JButton("Purple");
- JButton b5 = new JButton("Blue");
- JButton b6 = new JButton("Pink");
- JButton b7 = new JButton("Brown");
- f.add(b1);
- f.add(b2);
- f.add(b3);
- f.add(b4);
- f.add(b5);
- f.add(b6);
- f.add(b7);
- f.setLayout(new FlowLayout(FlowLayout.LEFT));
- f.setSize(400, 200);
- f.setVisible(true);
- }
- public static void main(String[] args) {
- new Flow();
- }
- }
Output
For other layout managers, please refer the link given below: