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

Java Unit 3 Notes 1

The document discusses Java programming and layout managers. It begins by listing topics to be covered, including layout managers, their requirements, types, and how they work. It then provides details on specific layout managers like FlowLayout and BorderLayout. It explains how FlowLayout arranges components from left to right, top to bottom. It also demonstrates how BorderLayout divides a container into five regions (north, south, east, west, center) and positions components accordingly. The document concludes by discussing how insets can be used in layouts to specify border spaces around a container.

Uploaded by

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

Java Unit 3 Notes 1

The document discusses Java programming and layout managers. It begins by listing topics to be covered, including layout managers, their requirements, types, and how they work. It then provides details on specific layout managers like FlowLayout and BorderLayout. It explains how FlowLayout arranges components from left to right, top to bottom. It also demonstrates how BorderLayout divides a container into five regions (north, south, east, west, center) and positions components accordingly. The document concludes by discussing how insets can be used in layouts to specify border spaces around a container.

Uploaded by

Manav Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Subject Name Java Programming (CSE III Year)

Department of Computer Science & Engineering


Created By: Dr. Avinash Dwivedi

JIMS Engineering Management Technical Campus


Greater Noida, UP - 201303
(Affiliated to Guru Gobind Singh Indraprastha University, New Delhi)
Recognized u/s 2(f) by UGC & Accredited with ‘A’ Grade by NAAC
Participant of UNGC & UNPRME, New York
ISO 9001:2015 Quality Certified
Subject: Java Programming
Topic: Layout Manager Interface, Default
layouts, insects and Dimension, Border
Layout
List of Topics to be covered

 Layout Managers
 Layout Managers Requirement
 Layout Managers working
 Layout Managers types
 Flow Layout
 BorderLayout
 Using Insets in layouts
Layout Managers

A layout manager is an object that implements


the LayoutManager interface and determines the size and position
of the components within a container.
Although components can provide size and alignment hints, a
container's layout manager has the final say on the size and
position of the components within the container.

The LayoutManagers are used to arrange components in a


particular manner. LayoutManager is an interface that is
implemented by all the classes of layout managers.
Layout Managers Requirement

Since the Component class defines the setSize() and setLocation() methods, all
Components can be sized and positioned with those methods.

Problem: the parameters provided to those methods are defined in terms of pixels.
Pixel sizes may be different (depending on the platform) so the use of those methods
tends to produce GUIs which will not display properly on all platforms.

Solution: Layout Managers. Layout managers are assigned to Containers. When a


Component is added to a Container, its Layout Manager is consulted in order to
determine the size and placement of the Component.

NOTE: If you use a Layout Manager, you can no longer change the size and location
of a Component through the setSize and setLocation methods.
Layout Managers

Layout Managers (java.awt.LayoutManager interface) -


Layout Managers are used to arrange different components (Button,
Label, TextField etc) within different types of windows (Panel, Applet,
Frame etc.).

They control the size and position of components within container.

Each Container object has a associated LayoutManager. A layout manager


is an object of any class that implements LayoutManager interface.
setLayout()

We can set the layout of any container by setLayout()


method.

It has the following syntax -

public void setLayout(LayoutManager obj)


Where obj is the object of desired layout manager
Layout Managers working

A layout manager automatically arranges your controls within a window by using


some type of algorithm.

It is very tedious to manually lay out a large number of components and sometimes
the width and height information is not yet available when you need to arrange some
control, because the native toolkit components haven't been realized.

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.
Layout Managers types

There are several different Layout Managers, each of which sizes and positions
its Components based on an algorithm:
FlowLayout
BorderLayout
GridLayout
CardLayout
Grid Bag Layout

For Windows and Frames, the default LayoutManager is BorderLayout.


For Panels, the default LayoutManager is FlowLayout.
Flow Layout

The algorithm used by the FlowLayout is to lay out Components like words on
a page: Left to right, top to bottom.

It fits as many Components into a given row before moving to the next row.
Panel aPanel = new Panel();

aPanel.add(new Button("Ok"));
aPanel.add(new Button("Add"));
aPanel.add(new Button("Delete"));
aPanel.add(new Button("Cancel"));
Flow Layout

It is the default layout for Panel and Applet.

In FlowLayout components are placed one after another in


a line in left to right or top to bottom direction, once the
line are filled component will be placed in the next line just
like text in any text editor.

There will be a small gap between components paced


within container by default; this gap can also be specified
by user.
FlowLayout defies the following constructors -

public FlowLayout()
It creates a layout where components will be centrally aligned, and there will be a gap
of 5 pixels between each components.
public FlowLayout(int alignment)
It creates a layout with the specified alignment, and there will be a gap of 5 pixels
between each components.
Alignment can have one of the following values -
FlowLayout.CENTRE
FlowLayout.LEFT
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
public FlowLayout(int alignment, int horz_gap, int vert_gap)
It creates a layout with specified alignment and horizontal and vertical gab between
components.
Flow Layout

Button b3 = new Button("30");


Button b4 = new Button("40");
import java.awt.*;
Button b5 = new Button("50");
import java.awt.event.*; Button b6 = new Button("60");
public class Test Button b7 = new Button("70");
Button b8 = new Button("80");
{
Button b9 = new Button("90");
public static void main(String args[]) Button b10 = new Button("100");
{ fr.add(b1);, fr.add(b2); fr.add(b3);
fr.add(b4); fr.add(b5); fr.add(b6);
Frame fr = new Frame("FlowLayout Demo");
fr.add(b7); fr.add(b8); fr.add(b9);
fr.setSize(300, 300); fr.add(b10);
fr.setVisible(true); fr.addWindowListener(new WindowAdapter()
FlowLayout fl = new FlowLayout(FlowLayout.LEFT); {
public void windowClosing(WindowEvent e)
// create a FlowLayout with Left alignment {
fr.setLayout(fl); System.exit(0);
// set FlowLayout to Frame }
Button b1 = new Button("10"); });
Button b2 = new Button("20"); }
}
BorderLayout

The BorderLayout Manager breaks the Container up into 5 regions (North,


South, East, West, and Center).
When Components are added, their region is also specified:

Frame aFrame = new Frame();


aFrame.add("North", new Button("Ok"));
aFrame.add("South", new Button("Add"));
aFrame.add("East", new Button("Delete"));
aFrame.add("West", new Button("Cancel"));
aFrame.add("Center", new Button("Recalculate"));
Border Layout (cont)

The regions of the BorderLayout are defined as follows:

North

West Center East

South
Program

import java.awt.*; Button b2= new Button("CENTER");


public class BorderDemo extends Frame{ add(b2, BorderLayout.CENTER);
public BorderDemo(String title) // constructor Button b3= new Button("LINE_START");
{ add(b3, BorderLayout.LINE_START);
// It would create the Frame by calling the Button b4= new Button("PAGE_END");
//constructor of Frame class. add(b4, BorderLayout.PAGE_END);
super(title); Button b5= new Button("LINE_END");
//Setting up Border Layout add(b5, BorderLayout.LINE_END);
setLayout(new BorderLayout()); }
Button b1 = new Button("PAGE_START"); public static void main(String[] args)
//Creating a button and adding it to PAGE_START area { BorderDemo bd = new
BorderDemo("Border
add(b1, BorderLayout.PAGE_START);
Layout GIMS");
// Similarly creating 4 other buttons and adding
bd.setSize(500,250);
// them to other 4 areas of Border Layout
bd.setVisible(true);
}}
Program (Contd..)
Insets in layouts
Using Insets in layouts

java.awt.Insets class object is a representation of the borders of a container. It


specifies the space that a container must leave at each of its edges. The space
can be a border, a blank space, or a title.

It has the following constructor –


public Insets(int top, int left, int bottom, int right)

The values top, left, bottom and right specify the space between Container
and its enclosing window.
To specify this space you need to override getInsets() method provided by
java.awt.Container class. It has the following general form -
public Insets getInsets()
It returns an object of Insets that define the desired space between Container
and its enclosing window.
Example

Here we are using the same example used in Border layout


with 20 pixels of space from all side.
Border Layout program and use of insets

import java.applet.Applet; add(b1, BorderLayout.CENTER);


import java.awt.*; add(b2, BorderLayout.NORTH);
import java.awt.event.*; add(b3, BorderLayout.SOUTH);
public class EventDemo2 extends Applet add(b4, BorderLayout.EAST);
{ add(b5, BorderLayout.WEST);
public void init()
{ }
BorderLayout bl = new BorderLayout();
// create a BorderLayout with no gap public Insets getInsets()
// between the components {
setLayout(bl); Insets ist = new Insets(20, 20, 20, 20);
// set BorderLayout to Frame
return ist;
}
Button b1 = new Button("CENTRE BTN");
}
Button b2 = new Button("NORTH BTN");
Button b3 = new Button("SOUTH BTN");
Button b4 = new Button("EAST BTN");
Button b5 = new Button("WEST BTN");
Thank You !!

You might also like