0% found this document useful (0 votes)
45 views

Advanced Java Lecture-8

Layouts in Java describe how components are arranged and positioned within containers. The key layout managers include FlowLayout, BorderLayout, GridLayout, and GridBagLayout. FlowLayout positions components left-to-right or top-to-bottom. BorderLayout divides the container into five regions (north, south, east, west, center). GridLayout divides the container into equally sized cells in rows and columns. GridBagLayout positions components using a grid system and GridBagConstraints to specify each component's position and size.

Uploaded by

cupidcallin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Advanced Java Lecture-8

Layouts in Java describe how components are arranged and positioned within containers. The key layout managers include FlowLayout, BorderLayout, GridLayout, and GridBagLayout. FlowLayout positions components left-to-right or top-to-bottom. BorderLayout divides the container into five regions (north, south, east, west, center). GridLayout divides the container into equally sized cells in rows and columns. GridBagLayout positions components using a grid system and GridBagConstraints to specify each component's position and size.

Uploaded by

cupidcallin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Layouts in JAVA

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

FlowLayout(int how, int hGap, int vGap)

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)

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.

public class BorderLayoutDemo extends JFrame


{
public BorderLayoutDemo()
{
setLayout(new BorderLayout());
add(new Button("This is across the top."),BorderLayout.NORTH);
add(new Label("The footer message might go here."),
BorderLayout.SOUTH);
add(new Button("Right"), BorderLayout.EAST);
add(new Button("Left"), BorderLayout.WEST);
String msg = "The reasonable man adapts
add(new TextArea(msg), BorderLayout.CENTER);
}
public static void main(String[] a)
{
abc obj=new abc();
obj.setVisible(true);
obj.pack();
}
}

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(int rows, int cols)


Divides the window into the specified number of rows and
columns
Either rows or cols (but not both) can be zero

GridLayout(int rows, int cols,


int hGap, int vGap)
Uses the specified gaps between cells
11

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:

How the component is laid out in the display area


In which cell the component starts and ends
How the component stretches when extra room is available
Alignment in cells

13

GridBagLayout: Basic Steps


Set the layout, saving a reference to it
GridBagLayout layout = new GridBagLayout();
setLayout(layout);

Allocate a GridBagConstraints object


GridBagConstraints constraints =
new GridBagConstraints();

Set up the GridBagConstraints for


component 1
constraints.gridx = x1;
constraints.gridy = y1;
constraints.gridwidth = width1;
constraints.gridheight = height1;

Add component 1 to the window, including constraints


add(component1, constraints);

Repeat the last two steps for each remaining component


14

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

GridBagConstraints Fields (Continued)


fill
Specifies what to do to an element that is smaller than the cell size
constraints.fill = GridBagConstraints.VERTICAL;

The size of row/column is determined by the widest/tallest element in


it
Can be NONE, HORIZONTAL, VERTICAL, or BOTH

anchor

If the fill is set to GridBagConstraints.NONE, then the anchor


field determines where the component is placed
constraints.anchor = GridBagConstraints.NORTHEAST;

Can be NORTH, EAST, SOUTH, WEST, NORTHEAST, NORTHWEST,


SOUTHEAST, or SOUTHWEST

18

You might also like