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

Join the conversation! Your thoughts help the community grow.