Layout Managers
1
The layout will specify the format or the order in which the components have got to
be placed on the container.
A layout manager automatically arranges your controls within a window.
Each Container object features a layout manager related to it.
AWT package provides following types of Layout Managers:
Flow Layout
Border Layout
Card Layout
Grid Layout
2
GridBag Layout
Flow Layout
This layout will display the components in sequence from left to right, from top to
bottom.
The components will always be displayed in the first-line and if the first line is filled,
these components are displayed on the next line automatically.
Note: If the row contains only one component then the component is aligned in the
center position of that row.
FlowLayout f1 = new FlowLayout();
FlowLayout f1 = new FlowLayout(int align);
3
FlowLayout f1 = new FlowLayout(int align, int hgap, int vgap);
Border Layout
This layout will display the components along the border of the container.
This layout contains five locations where the component can be displayed.
Locations are North, South, East, west, and Center.
BorderLayout bl = new BorderLayout();
BorderLayout bl = new BorderLayout(int vgap, int hgap);
4
Card Layout
A card layout represents a stack of cards displayed on a container.
At a time only one card can be visible and each can contain the only one component.
CardLayout cl = new CardLayout();
CardLayout cl = new CardLayout(int hgap, int vgap);
Methods of CardLayout in Java
first(Container): It is used to flip to the first card of the given container.
last(Container): It is used to flip to the last card of the given container.
next(Container): It is used to flip to the next card of the given container.
previous(Container): It is used to flip to the previous card of the given container.
show(Container, cardname): It is used to flip to the specified card with the given name.
5
Grid Layout
The layout will display the components in the format of rows and columns
The container will be divided into a table of rows and columns.
The intersection of a row and column cell and every cell contains only one
component and all the cells are of equal size.
According to Grid Layout Manager, the grid cannot be empty.
GridLayout gl = new GridLayout(int rows, int cols);
GridLayout gl = new GridLayout(int rows, int cols, int vgap, int hgap);
6
Grid Bag
In GridLayout manager there is no grid control i.e. inside a grid we cannot align the
component in a specific position.
In this layout, we can specify the location, specify the size.
In this Layout manager, for each grid, we need to define grid properties or
constraints.
Based on grid properties, the layout manager aligns a component on the grid, and
also we can span multiple grids also as per the requirement.
GridBagLayout gbl = new GridBagLayout();
7
The null layout
You do not need to specify the layout manager and you can explicitly set the sizes
and positions of the components.
Use a method setBounds()
8
class Test extends Jframe {
Jbutton btn; JTextField txt; JComboBox cb;
Test() {
btn = new Jbutton(“Ok”); txt= new JTextField(); cb = new JComboBox();
Jframe f= Jframe();
f.add(btn); f.add(txt); f.add(cb); f.setLayout(null);
btn.setBounds(50,20,60,30); txt.setBounds(50,90,60,20);
cb.setBounds(50,120,60,20); }
public static void main(String args[]){
Test n= new Test();
n.setVisible(true);
}} 9
Menu
The Menu class represents the pull-down menu component which is deployed from a
menu bar.
A JFrame can hold a menu bar to which the pull down menus are attached
Menus consists of menu items that the user can select
Classes
JMenuBar
Jmenu
JmenuItem
Method
10
setMnemonics()
An even can be defined as a type of signal to the program that something has
happened.
Delegation Event Model (DEM)
The modern approach to handle events
The application logic that process the event is separated from the user interface logic
that generates the events.
11
Delegation Event Model (DEM)
There are two objects in DEM
1. Source: is an object on which event occurs
2. Listener: used for generating response to an event
When using GUI you must perform two tasks for controlling an event created
1. A source must register listeners in order for the listener to receive events.
source.add <Type> Listener
b.addActionListener(this);
12
Delegation Event Model (DEM)
2. It must implements methods to receive and process notifications
class Sample implements ActionListener {
public void actionPerfomed(java.awt.event.ActionEvent evt)
{
}
}
13
Event classes
Event Classes Listener interface Method Source
ActionEvent ActionListener actionPerformed() Jbutton, JTextField…
ItemEvent ItemListener itemStateChange() JCheckBox, JRadioButton,
JComboBox
MouseEvent MouseListener mouseClicked()
mouseEntered()
mouseExited()
MouseMotionListener mouseMoved()
14
The java.io package contains nearly every class you might ever need to perform input
and output (I/O) in Java.
All these streams represent an input source and an output destination.
Streams
A stream can be defined as a sequence of data.
There are two kinds of Streams
1. InPutStream − The InputStream is used to read data from a source.
2. OutPutStream − The OutputStream is used for writing data to a destination.
15
The java.io package contains nearly every class you might ever need to perform input
and output (I/O) in Java.
All these streams represent an input source and an output destination.
Streams
A stream can be defined as a sequence of data.
There are two kinds of Streams
1. InPutStream − The InputStream is used to read data from a source.
2. OutPutStream − The OutputStream is used for writing data to a destination.
16
ByteStreams
Java byte streams are used to perform input and output of 8-bit.
Though there are many classes related to byte streams but the most frequently used classes
are, FileInputStream and FileOutputStream.
CharacterStreams
Java Character streams are used to perform input and output for 16-bit.
Though there are many classes related to character streams but the most frequently used
classes are, FileReader and FileWriter.
17
Standard Streams
All the programming languages provide support for standard I/O where the user's program
can take input from a keyboard and then produce an output on the computer screen.
Java provides the following three standard streams.
Standard Input − This is used to feed the data to user's program and usually a keyboard is used as
standard input stream and represented as System.in.
Standard Output − This is used to output the data produced by the user's program and usually a
computer screen is used for standard output stream and represented as System.out.
Standard Error − This is used to output the error data produced by the user's program and usually a
18
computer screen is used for standard error stream and represented as System.err.
Buffered Streams
Most of the class we've seen so far use unbuffered I/O
This means each read or write request is handled directly by the underlying OS.
This can make a program much less efficient, since each such request often triggers disk access, network activity,
or some other operation that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams.
Buffered input streams read data from a memory area known as a buffer.
Similarly, buffered output streams write data to a buffer.
Used Classes :
BufferedInputStream and BufferedOutputStream
19
BufferedReader and BufferedWriter