Layout refers to the arrangement of components
within the container. The task of laying out the controls is done automatically
by the Layout Manager.
The following are the layout managers:
- FlowLayout
- BorderLayout
- CardLayout
- BoxLayout
- GridLayout
- GridBagLayout
- GroupLayout
- SpringLayout
Ø Java
BorderLayout
The BorderLayout is used to arrange the components
in five regions: north, south, east, west, and center. Each region (area) may
contain one component only. It is the default layout of a frame or window. The
BorderLayout provides five constants for each region:
- public
static final int NORTH
- public
static final int SOUTH
- public
static final int EAST
- public
static final int WEST
- public
static final int CENTER
Constructors of BorderLayout class:
- BorderLayout(): creates a border layout but with no gaps between the
components.
- BorderLayout(int
hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.
Ex:
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new
JFrame();
//
creating buttons
JButton b1
= new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2
= new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3
= new JButton("EAST");; // the button will be labeled as EAST
JButton b4
= new JButton("WEST");; // the button will be labeled as WEST
JButton b5
= new JButton("CENTER");; // the button will be labeled as
CENTER
f.add(b1,
BorderLayout.NORTH); // b1 will be placed in the North Direction
f.add(b2,
BorderLayout.SOUTH); // b2 will be
placed in the South Direction
f.add(b3,
BorderLayout.EAST); // b2 will be placed
in the East Direction
f.add(b4,
BorderLayout.WEST); // b2 will be placed
in the West Direction
f.add(b5,
BorderLayout.CENTER); // b2 will be
placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new
Border();
}
}
Ø Java GridLayout
The Java GridLayout class is used to arrange the
components in a rectangular grid. One component is displayed in each rectangle.
Constructors of GridLayout class
- GridLayout(): creates a grid layout with one column per component in a row.
- GridLayout(int
rows, int columns): creates a grid
layout with the given rows and columns but no gaps between the components.
- GridLayout(int
rows, int columns, int hgap, int vgap): creates
a grid layout with the given rows and columns along with given horizontal
and vertical gaps.
Ex:
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample
{
JFrame frameObj;
GridLayoutExample()
{
frameObj = new JFrame();
// creating 9 buttons
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
// adding buttons to the frame
// since, we are using the parameterless
constructor, therfore;
// the number of columns is equal to the number of
buttons we
// are adding to the frame. The row count remains
one.
frameObj.add(btn1); frameObj.add(btn2);
frameObj.add(btn3);
frameObj.add(btn4); frameObj.add(btn5);
frameObj.add(btn6);
frameObj.add(btn7); frameObj.add(btn8);
frameObj.add(btn9);
// setting the grid layout using the parameterless
constructor
frameObj.setLayout(new GridLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new GridLayoutExample();
}
}
Ø Java FlowLayout
The Java FlowLayout class is used to arrange the
components in a line, one after another (in a flow). It is the default layout
of the applet or panel.
Fields of FlowLayout class
- public
static final int LEFT
- public
static final int RIGHT
- public static
final int CENTER
- public
static final int LEADING
- public
static final int TRAILING
Constructors of FlowLayout class
- FlowLayout(): creates a flow layout with centered alignment and a default 5
unit horizontal and vertical gap.
- FlowLayout(int
align): creates a flow layout with the given
alignment and a default 5 unit horizontal and vertical gap.
- FlowLayout(int
align, int hgap, int vgap): creates
a flow layout with the given alignment and the given horizontal and
vertical gap.
Ex:
// import statements
import java.awt.*;
import javax.swing.*;
public class FlowLayoutExample
{
JFrame frameObj;
// constructor
FlowLayoutExample()
{
//
creating a frame object
frameObj =
new JFrame();
//
creating the buttons
JButton b1
= new JButton("1");
JButton b2
= new JButton("2");
JButton b3 = new JButton("3");
JButton b4
= new JButton("4");
JButton b5
= new JButton("5");
JButton b6
= new JButton("6");
JButton b7
= new JButton("7");
JButton b8
= new JButton("8");
JButton b9
= new JButton("9");
JButton
b10 = new JButton("10");
// adding
the buttons to frame
frameObj.add(b1); frameObj.add(b2); frameObj.add(b3);
frameObj.add(b4);
frameObj.add(b5); frameObj.add(b6);
frameObj.add(b7);
frameObj.add(b8);
frameObj.add(b9);
frameObj.add(b10);
//
parameter less constructor is used therefore, alignment is center
//
horizontal as well as the vertical gap is 5 units.
frameObj.setLayout(new
FlowLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new
FlowLayoutExample();
}
}
Ø Java 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:
- public
static final int X_AXIS: Alignment
of the components are horizontal from left to right.
- public
static final int Y_AXIS: Alignment
of the components are vertical from top to bottom.
- 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.
- 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.
Constructor of BoxLayout class
- BoxLayout(Container
c, int axis): creates a box layout that arranges the
components with the given axis.
Ø Java 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
- CardLayout(): creates a card layout with zero horizontal and vertical gap.
- CardLayout(int
hgap, int vgap): creates a card layout with the given
horizontal and vertical gap.
Commonly Used Methods of CardLayout Class
- public void
next(Container parent): is
used to flip to the next card of the given container.
- public void
previous(Container parent): is
used to flip to the previous card of the given container.
- public void
first(Container parent): is
used to flip to the first card of the given container.
- public void
last(Container parent): is
used to flip to the last card of the given container.
- public void
show(Container parent, String name): is
used to flip to the specified card with the given name.
Ø Java
GridBagLayout
The Java GridBagLayout class is used to align
components vertically, horizontally or along their baseline.
The components may not be of the same size. Each
GridBagLayout object maintains a dynamic, rectangular grid of cells. Each
component occupies one or more cells known as its display area. Each component
associates an instance of GridBagConstraints. With the help of the constraints
object, we arrange the component's display area on the grid. The GridBagLayout
manages each component's minimum and preferred sizes in order to determine the
component's size. GridBagLayout components are also arranged in the rectangular
grid but can have many different sizes and can occupy multiple rows or columns.
Constructor:
GridBagLayout(): The parameterless constructor is used to create a grid bag layout
manager.
Ø GroupLayout
GroupLayout groups its components and places them in a Container
hierarchically. The grouping is done by instances of the Group
class.
Group is an abstract class, and two concrete classes
which implement this Group class are SequentialGroup and ParallelGroup.
SequentialGroup positions its child sequentially one
after another whereas ParallelGroup aligns its child on top of each other.
The GroupLayout class provides methods such as
createParallelGroup() and createSequentialGroup() to create groups.
GroupLayout treats each axis independently. That is,
there is a group representing the horizontal axis, and a group representing the
vertical axis. Each component must exist in both a horizontal and vertical
group, otherwise an IllegalStateException is thrown during layout or when the
minimum, preferred, or maximum size is requested.
Ø Java
SpringLayout
A SpringLayout arranges the
children of its associated container according to a set of constraints.
Constraints are nothing but horizontal and vertical distance between
two-component edges. Every constraint is represented by a
SpringLayout.Constraint object.
Each child of a SpringLayout container, as well as
the container itself, has exactly one set of constraints associated with them.
Each edge position is dependent on the position of
the other edge. If a constraint is added to create a new edge, than the
previous binding is discarded. SpringLayout doesn't automatically set the
location of the components it manages.
Constructor:
SpringLayout(): The default constructor of the class is used to instantiate the
SpringLayout class
Ø ScrollPaneLayout
The layout manager is used by JScrollPane.
JScrollPaneLayout is responsible for nine components: a viewport, two
scrollbars, a row header, a column header, and four "corner"
components.
Constructor:
ScrollPaneLayout(): The parameterless constructor is used to create a new
ScrollPanelLayout.