Layout Manager
Layout Manager
Layout Manager
While it is possible to lay out Java controls by hand, you generally won’t
want to, for two main reasons.
First, it is very tedious to manually lay out a large number of components.
Second, sometimes the width and height information is not yet available
when you need to arrange some control.
Each Container object has a layout manager associated with it.
A layout manager is an instance of any class that implements the
LayoutManager interface.
The layout manager is set by the setLayout( ) method.
If no call to setLayout( ) is made, then the default layout manager is
used.
Whenever a container is resized (or sized for the first time), the layout
manager is used to position each of the components within it.
The setLayout( ) method has the following general form:
void setLayout(LayoutManager layoutObj)
Each layout manager keeps track of a list of components that are stored
by their names.
The layout manager is notified each time you add a component to a
container.
Whenever the container needs to be resized, the layout manager is
consulted via its minimumLayoutSize( ) and preferredLayoutSize( )
methods.
Each component that is being managed by a layout manager contains the
getPreferredSize( ) and getMinimumSize( ) methods.
These return the preferred and minimum size required to display each
component.
FlowLayout
FlowLayout is the default layout manager.
This is the layout manager that the preceding examples have used.
FlowLayout implements a simple layout style, which is similar to how
words flow in a text editor.
The direction of the layout is governed by the container’s component
orientation property, which, by default, is left to right, top to bottom.
Therefore, by default, components are laid out line-by-line beginning at
the upper-left corner.
In all cases, when a line is filled, layout advances to the next line.
A small space is left between each component, above and below, as well
as left and right.
add(winXP);
add(winVista);
add(solaris);
add(mac);
BorderLayout
The BorderLayout class implements a common layout style for top-level
windows.
It has four narrow, fixed-width components at the edges and one large
area in the center.
The four sides are referred to as north, south, east, and west.
The middle area is called the center.
Here are the constructors defined by BorderLayout:
BorderLayout( )
BorderLayout(int horz, int vert)
When adding components, you will use these constants with the
following form of add( ), which is defined by Container:
Example
// Demonstrate BorderLayout.
import java.awt.*;
import java.applet.*;
import java.util.*;
/*
<applet code="BorderLayoutDemo" width=400 height=200>
</applet>
*/
public class BorderLayoutDemo extends Applet {
public void init() {
setLayout(new BorderLayout());
Here is a sample program that creates a 4×4 grid and fills it in with 15 buttons,
each labeled with its index:
// Demonstrate GridLayout
import java.awt.*;
import java.applet.*;
/*
<applet code="GridLayoutDemo" width=300 height=200>
</applet>
*/
public class GridLayoutDemo extends Applet {
static final int n = 4;
BoxLayout
The Java BoxLayout class is used to arrange the components either vertically or
horizontally. For this purpose, the BoxLayout class provides four constants. They are
as follows:
Note: The BoxLayout class is found in javax.swing package.
1. public static final int X_AXIS: Alignment of the components are horizontal from left
to right.
2. public static final int Y_AXIS: Alignment of the components are vertical from top to
bottom.
3. public static final int LINE_AXIS: Alignment of the components is similar to the way
words are aligned in a line, which is based on the ComponentOrientation property of
the container. If the ComponentOrientation property of the container is horizontal,
then the components are aligned horizontally; otherwise, the components are aligned
vertically. For horizontal orientations, we have two cases: left to right, and right to left.
If the value ComponentOrientation property of the container is from left to right,
then the components are rendered from left to right, and for right to left, the
rendering of components is also from right to left. In the case of vertical orientations,
the components are always rendered from top to bottom.
4. public static final int PAGE_AXIS: Alignment of the components is similar to the
way text lines are put on a page, which is based on the ComponentOrientation
property of the container. If the ComponentOrientation property of the container is
horizontal, then components are aligned vertically; otherwise, the components are
aligned horizontally. For horizontal orientations, we have two cases: left to right, and
right to left. If the value ComponentOrientation property of the container is also from
left to right, then the components are rendered from left to right, and for right to left,
the rendering of components is from right to left. In the case of vertical orientations,
the components are always rendered from top to bottom.
import java.awt.*;
import javax.swing.*;
public BoxLayoutExample1 () {
buttons = new Button [5];
Output:
Example of BoxLayout class with X-AXIS
FileName: BoxLayoutExample2.java
import java.awt.*;
import javax.swing.*;
public BoxLayoutExample2() {
buttons = new Button [5];
Output:
CardLayout
The Java CardLayout class manages the components in such a manner that only one
component is visible at a time. It treats each component as a card that is why it is
known as CardLayout.
Constructors of CardLayout Class
1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and
vertical gap.
o public void next(Container parent): is used to flip to the next card of the
given container.
o public void previous(Container parent): is used to flip to the previous card
of the given container.
o public void first(Container parent): is used to flip to the first card of the
given container.
o public void last(Container parent): is used to flip to the last card of the
given container.
o public void show(Container parent, String name): is used to flip to the
specified card with the given name.
// import statements
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
CardLayout crd;
cPane = getContentPane();
cPane.setLayout(crd);
// adding listeners to it
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
// Upon clicking the button, the next card of the container is shown
// after the last card, again, the first card of the container is shown upon clicking
crd.next(cPane);
}
// main method
public static void main(String argvs[])
{
// creating an object of the class CardLayoutExample1
CardLayoutExample1 crdl = new CardLayoutExample1();
Output:
Again, we reach the first card of the container if the cat button is clicked, and the
cycle continues.
null Layout
null layout is not a real layout manager. It means that no layout manager is assigned and
the components can be put at specific x,y coordinates.
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;