Layout Manager
Layout Manager
manager.
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
SYNTAX
void setLayout(LayoutManager layoutObj)
Java has several predefined LayoutManager classes
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.
import java.awt.*;
import javax.swing.*;
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//setting flow layout of right alignment
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyFlowLayout();
}
}
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)
The first form creates a default border layout.
The second allows you to specify the horizontal and vertical space left between components in horz
and vert, respectively.
• BorderLayout defines the following constants that specify the regions: BorderLayout.CENTER
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.NORTH
• When adding components, you will use these constants with the following form of add( ), which is defined by
Container:
void add(Component compObj, Object region)
Here, compObj is the component to be added, and region specifies where the component will be added
import java.awt.*;
{
BorderLayoutExample()
setLayout(new BorderLayout());
add(new Button("NORTH"),BorderLayout.NORTH);
add(new Button("SOUTH"),BorderLayout.SOUTH);
add(new Button("EAST"),BorderLayout.EAST);
add(new Button("WEST"),BorderLayout.WEST);
add(new Button("CENTER"),BorderLayout.CENTER);
class BorderLayoutJavaExample
frame.setSize(400,150);
frame.setVisible(true);
}
GridLayout
GridLayout lays out components in a two-dimensional grid.
When you instantiate a GridLayout, you define the number of rows and columns.
The constructors supported by GridLayout are shown here:
GridLayout( )
GridLayout(int numRows, int numColumns)
GridLayout(int numRows, int numColumns, int horz, int vert)
The first form creates a single-column grid layout.
The second form creates a grid layout with the specified number of rows and columns.
The third form allows you to specify the horizontal and vertical space left between components in horz
and vert, respectively. Either numRows or numColumns can be zero. Specifying numRows as zero
allows for unlimited-length columns.
import java.awt.*;
GridLayoutExample()
setLayout(new GridLayout(4,3));
add(button[i]);
class GridLayoutJavaExample
frame.setSize(400,150);
frame.setVisible(true);
}
CardLayout
The CardLayout class is unique among the other layout managers in that it stores several different
layouts.
Each layout can be thought of as being on a separate index card in a deck that can be shuffled so
that any card is on top at a given time.
This can be useful for user interfaces with optional components that can be dynamically enabled
and disabled upon user input. You can prepare the other layouts and have them hidden, ready to be
activated when needed.
CardLayout provides these two constructors:
CardLayout( )
CardLayout(int horz, int vert)
The first form creates a default card layout.
The second form allows you to specify the horizontal and vertical space left between components
in horz and vert, respectively
void add(Component panelObj, Object name)
import java.awt.*;
import java.awt.event.*;
CardLayoutExample()
setLayout(card);
add(Btnfirst,"Card1");
add(BtnSecond,"Card2");
add(BtnThird,"Card3");
Btnfirst.addActionListener(this);
BtnSecond.addActionListener (this);
BtnThird.addActionListener(this);
}
card.next(this);
class CardLayoutJavaExample
frame.setSize(220,150);
frame.setResizable(false);
frame.setVisible(true);
}
GridBagLayout
What makes the grid bag useful is that you can specify the relative placement of components by specifying
their positions within cells inside a grid. The key to the grid bag is that each component can be a different size,
and each row in the grid can have a different number of columns. This is why the layout is called a grid bag.
It’s a collection of small grids joined together.
GridBagLayout defines only one constructor, which is shown here:
GridBagLayout( )
What is an Event?
Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as
result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse,