Components and Event Handling
Components and Event Handling
AND EVENT
HANDLING AND
LAYOUT
TYPES OF LAYOUTS
1. FlowLayout :-
The flow layout, is the most basic layout manager in which
components are placed from left to right, as they were added. When the
horizontal row is too small, to put all the components in one row, then it uses
multiple rows. You can align the components left, right, or center (default).
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.
Example:- public class DEMO
{ public static void main(String[] args)
import java.awt.*; {
class MyFlowLayout new MyFlowLayout();
{ Frame f; }
MyFlowLayout() }
{ f=new Frame();
Button b1=new Button("1"); Output:-
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setLayout(new
FlowLayout(FlowLayout.RIGHT));
f.setSize(300,300);
f.setVisible(true);
}
}
2. BorderLayout :-
In the Border Layout Manager, the components are positioned in
five different areas. In other words, North, South, East, West and Center.
Each area may contain only one component. If you enlarge the window, you
will notice that the center area gets as much of the newly available space, as
possible. The other area will expand, only as much as necessary, to keep the
available space filled.
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.
Example :- public class DEMO
{ public static void main(String[] args)
import java.awt.*; {
class BorLayout new BorLayout();
{ BorLayout() }
{ Frame f=new Frame(); }
f.setLayout(new BorderLayout());
Button b1 = new Button("NORTH"); Output :-
Button b2 = new Button("SOUTH");
Button b3 = new Button("EAST");
Button b4 = new Button("WEST");
Button b5 = new Button("CENTER");
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
}
}
3. GridLayout :-
Grid Layout is used to place the components in a grid of cells
(rectangular). The layout manager divides the container into a grid, so that
components can be placed in rows and columns. Each component will have
the same width and height. The components are added to the grid starting at
the top-left cell and proceeding left-to-right, until the row is full. Then go to
the next row.
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 alongwith given horizontal and
vertical gaps.
Example :- f.setLayout(new GridLayout());
f.setSize(300,300);
import java.awt.*; f.setVisible(true);
class MyGridLayout }
{ MyGridLayout() }
{ Frame f=new Frame(); public class DEMO2
Button b1 = new Button("1"); { public static void main(String[] args)
Button b2 = new Button("2"); {
Button b3 = new Button("3"); new MyGridLayout();
Button b4 = new Button("4"); }
Button b5 = new Button("5"); }
Button b6 = new Button("6");
Button b7 = new Button("7"); Output :-
Button b8 = new Button("8");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
4. GridBagLayout :-
GridBagLayout is one of the most flexible — and complex —
layout managers the Java platform provides. A GridBagLayout places
components in a grid of rows and columns, allowing specified components to
span multiple rows or columns. Not all rows necessarily have the same
height. Similarly, not all columns necessarily have the same width.
Essentially, GridBagLayout places components in rectangles (cells) in a grid,
and then uses the components' preferred sizes to determine how big the cells
should be.
Example :- gcon.gridx = 0;
import java.awt.*; gcon.gridy = 0;
gcon.gridheight=1;
class MyGridBagLayout extends Frame gcon.gridwidth = 2;
{ gb.setConstraints(b1, gcon);
public MyGridBagLayout() add(b1);
{
Button b1 = new Button("Btn 1"); gcon.gridx = 2;
Button b2 = new Button("Btn 2"); gb.setConstraints(b2, gcon);
Button b3 = new Button("Btn 3"); add(b2);
Button b4 = new Button("Btn 4");
Button b5 = new Button("Btn 5"); gcon.gridx = 0;
Button b6 = new Button("Btn 6"); gcon.gridy = 1;
Button b7 = new Button("Btn 7"); gcon.gridwidth = 1;
Button b8 = new Button("Btn 8"); gb.setConstraints(b3, gcon);
Button b9 = new Button("Btn 9"); add(b3);
gcon.gridx = 0; }
gcon.gridy = 3; }
Output :-
gcon.gridwidth = 3;
gb.setConstraints(b8, gcon);
add(b8);
gcon.gridx = 3;
gcon.gridy = 2;
gcon.gridheight=2;
gcon.gridwidth = 1;
gb.setConstraints(b9, gcon);
add(b9);
5. CardLayout :-
The 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.
1. Label :- A label is a user for placing text inside the container. A label
is used only for inputting text. The label does not imply that the text
can be altered or it can be used as a button which can be further
worked upon.
Label n=new Label("Name:",Label.CENTER);
2. Button :- This command generates a button in the User Interface.
Clicking on the button would move the command to another page or
another web server which is used to show several other outputs in
the user interface page.
Button b1=new Button("submit");
6. Text Field :- A text field is used for the editing of a particular line of
text which can be used within the programming concept.
TextField t1 = new TextField(20);
7. Text Area:- A text area is used for the editing of multiple lines of
text. The only difference between the Text field and Text area is that
Text Field is used for editing a single line of text within the user
interface while a Text Area is used for editing multiple lines of text.
Output:-
ITEMEVENT CLASS
The Java ItemListener is notified whenever you click on the checkbox.
It is notified against ItemEvent.
The ItemListener interface is found in java.awt.event package.
It has only one method: itemStateChanged().
The itemStateChanged() method is invoked automatically whenever
you click or unclick on the registered checkbox component.
add(l);
add(t);
setSize(300,300);
setLayout(null);
setVisible(true);
setTitle("TextEvent Example");
t.addTextListener(this);
}
WINDOWEVENT CLASS
The object of this class represents the change in state of a window.
This low-level event is generated by a Window object when it is
opened, closed, activated, deactivated, or when focus is transferred into
or out of the Window.
The signature of 7 methods found in WindowListener interface are
given below :-
public abstract void windowActivated(WindowEvent e);
public abstract void windowClosed(WindowEvent e);
public abstract void windowClosing(WindowEvent e);
public abstract void windowDeactivated(WindowEvent e);
public abstract void windowDeiconified(WindowEvent e);
public abstract void windowIconified(WindowEvent e);
public abstract void windowOpened(WindowEvent e);
Example :- public void windowDeactivated(WindowEvent
import java.awt.*; we)
MyWindow() { System.out.println("deiconified"); }
{ addWindowListener(this);
setSize(400,400); public void windowIconified(WindowEvent
we)
setLayout(null);
{ System.out.println("iconified"); }
setVisible(true);
}
public void windowOpened(WindowEvent we)
public void windowActivated(WindowEvent
we) { System.out.println("opened"); }
{ System.out.println("activated"); } }
public class MyWindowEvent
{ System.out.println("closing"); }
dispose(); } }
ADAPTER CLASS
Java adapter classes provide the default implementation of
listener interfaces.
If you inherit the adapter class, you will not be forced to provide the
implementation of all the methods of listener interfaces.
So it saves code. The Adapter classes with their corresponding listener
interfaces are given below.
WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
WindowAdapter Example :-
import java.awt.*;
import java.awt.event.*;
class MyWindow extends Frame
{ MyWindow()
{
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ dispose(); }
});
setSize(400,400);
setLayout(null);
setVisible(true);
}
}
public class MyWindowEvent
{
public static void main(String[] args)
{
MyWindow a = new MyWindow();
}
}
KeyAdapter Example:- a.addKeyListener(new KeyAdapter()
{
import java.awt.*; public void keyTyped(KeyEvent ke)
import java.awt.event.*; {