Layout Managers
Layout Managers
A layout manager is an object that implements the Layout Manager interface* and
determines the size and position of the components within a container. Although
components can provide size and alignment hints, a container's layout manager
has the final say on the size and position of the components within the container.
The layout manager's job is to fit the components into the available area, while
maintaining the proper spatial relationships between the components.
Every container has a default layout manager; therefore, when you make a new
container, it comes with a LayoutManager object of the appropriate type.
Each JPanel object is initialized to use a FlowLayout, unless you specify differently
when creating the JPanel. Content panes use BorderLayout by default. If you do
not like the default layout manager that a panel or content pane uses, you are
free to change it to a different one
You can install a new layout manager at any time with the setLayout() method.
Below, we set the layout manager of a container to a BorderLayout:
setLayout ( new BorderLayout() );
1. BorderLayout
2. FlowLayout
3. GridLayout
4. BoxLayout
1.BorderLayout
The BorderLayout divides the container into five areas which
include: PAGE_START, PAGE_END, LINE_START, CENTER and LINE_END.
You can use the constants of BorderLayout class to indicate the area where you
want to place a component. If you enlarge the window, you can see that the
center area take up as much of space as possible and the other areas only expand
to fill available space.
You can set the horizontal and vertical gap between components by using
methods setHgap() and setVgap().
The BorderLayout is look like as
import java.awt.*;
import javax.swing.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,200);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
2.FlowLayout
FlowLayout is the simplest layout in Java Swing layouts. The FlowLayout places
components from left to right in a row using preferred component sizes until no
space is available in the container. When no space is available, a new row is
started. The placement of the component depends on the size of the container
therefore you cannot guarantee which row the component is placed.
The FlowLayout is useful as a pad for a single component to make sure that the
component will be placed at the center area of a container. Notice that
FlowLayout is default layout of JPanel container.
The FlowLayout application is look like as
import java.awt.*;
import javax.swing.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,150);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
3.GridLayout
GridLayout places components in a cell of a grid. Each cell has the same size
therefore each component takes up the same space in a container. When user
adjusts the container, the size of each component changes accordingly.
Here are the constructors of the GridLayout class:
Constructors Description
public GridLayout(int rows, int cols) Creates a grid layout with a given number
of rows and columns. If cols or rows is zero,
Constructors Description
any numbers of components can be placed
in a column or in a row.
Creates a grid layout with a given number
public GridLayout(int rows, int cols, int of rows and columns. Beside this, you can
hgap, int vgap) initialize the vertical and horizontal gap
between each of rows and columns.
The GridLayout application is look like as
import java.awt.*;
import javax.swing.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,150);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
4.BoxLayout
The BoxLayout is used to arrange the components either vertically or horizontally.
1. Box class offers a container that uses BoxLayout as its default layout
manager.
2. BoxLayout works to honor each component's x and y alignment properties
as well as its maximum size.
The BoxLayout class has only one constructor
public BoxLayout(java.awt.Container target, int axis)
1. 'target' argument specifies the container that needs to be laid out.
2. 'axis' specifies the axis to lay out components along.
The value of axis can be one of the following:
1. BoxLayout.X_AXIS
2. BoxLayout.Y_AXIS
3. BoxLayout.LINE_AXIS
4. BoxLayout.PAGE_AXIS
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
frame.setVisible(true);
}
}
Output: