Advanced Java Lecture-8
Advanced Java Lecture-8
Layout
Layout means the arrangement of
components within the container. In other
way we can say that placing the components
at a particular position within the container.
The task of layouting the controls is done
automatically by the Layout Manager.
Layout Managers
A Layout Manager directs the placement of Components
within a Container.
Each container has a layout manager, which is responsible for
arranging the components in a container.
The container's setLayout method can be used to set a layout
manager.
The layout manager places the components according to the
layout manager's rules, property settings and the constraints
associated with each component.
Each layout manager has a particular set of rules specific to
that layout manager.
Layout Managers
FlowLayout
FlowLayout
Simplest layout manager
Components are placed left to right in the order they are added
If there is not enough space for all the components to fit, they'll be moved
to the next line.
Components can be left aligned, centered or right aligned
Constructors
FlowLayout()
This creates the default layout, which centers components and leaves five pixels of space
between each component
- FlowLayout(int how)
This form lets you specify how each line is aligned. Valid values for how are as follows: FlowLayout.LEFT,
FlowLayout.RIGHT, FlowLayout.CENTER
Specify the alignment as well as the horizontal and vertical spacing between components
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class LabelnText
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Hello Swing");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(new JLabel("Diving into swing!"));
frame.add(new JTextField("Type something here")); frame.setVisible(true);
}
}
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.
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:
void add(Component compObj, Object region);
Here, compObj is the component to be added, and region specifies where the
component will be added.
GridLayout
GridLayout
Divides window into equal-sized rectangles based upon the number of rows and columns
specified
Items placed into cells left-to-right, top-to-bottom, based on the order added to the container
Ignores the preferred size of the component; each component is resized to fit into its grid cell.
So, Every component has the same width and height
Too few components results in blank cells
Too many components results in extra columns
10
GridLayout (Continued)
Constructors
GridLayout()
Creates a single row with one column allocated per component
GridLayout, Example
public class GridTest extends Applet
{
public void init()
{
setLayout(new GridLayout(2,3)); // 2 rows, 3 cols
add(new Button("Button One"));
add(new Button("Button Two"));
add(new Button("Button Three"));
add(new Button("Button Four"));
add(new Button("Button Five"));
add(new Button("Button Six"));
}
}
GridBagLayout
Behavior
Divides the window into grids, without requiring the
components to be the same size
Each component managed by a grid bag layout is
associated with an instance of GridBagConstraints
The GridBagConstraints specifies:
13
GridBagConstraints
Copied when component added to window
Thus, can reuse the GridBagConstraints
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = x1;
constraints.gridy = y1;
constraints.gridwidth = width1;
constraints.gridheight = height1;
add(component1, constraints);
constraints.gridx = x1;
constraints.gridy = y1;
add(component2, constraints);
15
GridBagConstraints Fields
gridx, gridy
Specifies the top-left corner of the component
Upper left of grid is located at (gridx, gridy)=(0,0)
Set to GridBagConstraints.RELATIVE to
auto-increment row/column
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = GridBagConstraints.RELATIVE;
container.add(new Button("one"), constraints);
container.add(new Button("two"), constraints);
16
GridBagConstraints Fields
(Continued)
gridwidth, gridheight
Specifies the number of columns and rows the Component occupies
constraints.gridwidth = 3;
GridBagConstraints.REMAINDER lets the component take up
the remainder of the row/column
weightx, weighty
Specifies how much the cell will stretch in the x or y direction if space
is left over
constraints.weightx = 3.0;
Constraint affects the cell, not the component (use fill)
Use a value of 0.0 for no expansion in a direction
Values are relative, not absolute
17
anchor
18