Chapter 3 Event Handling
Chapter 3 Event Handling
Chapter 3
Event Handling & Event Delegation Model
Thought of a Day
2
Delegation Event Model
Event: Action Preformed or state is changed.eg key is
pressed, mouse is clicked, Checkbox is selected
Package: java.awt.event
Model requires 1.Event, 2. EventSource,
3.EventListener
Steps:
Select the Event
Implement appropriate Listener
Register the event with concern source
Select the method of listener class and write code fr event
3
Delegation Event Model-figure
Event Listener
output object
Prepared By Dr. S. H. Kulkarni 4
Events –Source, Class, Listener
Sr. No. Source of Event Event class Event Listener Methods
5
Events –Source, Class, Listener
Sr. No. Source of Event Event class Event Listener Methods
6
Events –Source, Class, Listener
Sr. No. Source of Event Event class Event Listener Methods
7. Window
WindowEvent WindowListener 1. void windowActivated(WindowEvent
e);
2. void windowClosed(WindowEvent e);
3. void windowClosing(WindowEvent e)
;
4. void windowDeactivated(WindowEve
nt e);
5. void windowDeiconified(WindowEve
nt e);
6.void windowIconified(WindowEvent e
);
7. void windowOpened(WindowEvent e)
;
8
Button as a source of event
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
}
9
Output
10
Thought of a Day
11
Button as a Source of Event
12
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
int sum=n1+n2;
t3.setText(Integer.toString(sum));
}
}
13
Thought of a Day
14
Checkbox as a source of Event
15
Algorithm
1. Source-Checkbox
2. Event Class-?
3. Event Listener-?
4. Create a public class & implement Event Listener
5. Design GUI using applet
6. Register an Event
7. Call methods of Listener class
16
Thought of a Day
17
Algorithm
1. Source-Mouse
2. Event Class-?
3. Event Listener-?
4. Create a public class & implement Event Listener
5. Register an Event
6. Call methods of Listener class
18
Mouse as a source of Event
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/*<applet code="MouseListenerDemo.class" height=300 width=200>
</applet>*/
public class MouseListenerDemo extends Applet implements
MouseListener, MouseMotionListener
//implements the listeners
{
String msg="";
int mousex=0,mousey=0;
public void init()
{
addMouseListener(this); //register the event
addMouseMotionListener(this);
}
19
// Calling listener methods
public void mouseClicked(MouseEvent e)
{
mousex=0;
mousey=10;
msg="Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent e)
{
mousex=0;
mousey=10;
msg="Mouse Entered";
repaint();
}
20
public void mouseExited(MouseEvent e)
{
mousex=0;
mousey=10;
msg="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent e)
{
mousex=e.getX();
mousey=e.getY();
msg="Mouse Down";
repaint();
}
21
public void mouseReleased(MouseEvent e)
{
mousex=e.getX();
mousey=e.getY();
msg="Mouse up";
showStatus("Dragging Mouse at" +mousex+","+mousey);
repaint();
}
public void mouseDragged(MouseEvent e)
{
mousex=e.getX();
mousey=e.getY();
msg="*";
showStatus("Dragging Mouse at" +mousex+","+mousey);
repaint();
}
22
public void mouseMoved(MouseEvent e)
{
showStatus("Moving Mouse at"
+mousex+","+mousey);
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,mousex,mousey);
}
}
23
Output of a Program
24
25
KeyEvent Class
26
Key Event Constants
1.public abstract void windowActivated(WindowEvent e);
2.public abstract void windowClosed(WindowEvent e);
3.public abstract void windowClosing(WindowEvent e);
4.public abstract void windowDeactivated(WindowEvent e);
5.public abstract void windowDeiconified(WindowEvent e);
6.public abstract void windowIconified(WindowEvent e);
7.public abstract void windowOpened(WindowEvent e);
33
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowExample extends Frame implements Windo
wListener{
WindowExample(){
addWindowListener(this);
setSize(400,400);
setLayout(null);
setVisible(true);
}
34
public static void main(String[] args) {
new WindowExample();
}
public void windowActivated(WindowEvent arg0) {
System.out.println("activated");
}
public void windowClosed(WindowEvent arg0) {
System.out.println("closed");
}
35
What is next?
36
JPasswordField
37
Commonly used method of JPasswordField
JPasswordField.
3.String getPassword() : returns the text contained in
JPasswordField.
4.String getText() : returns the text contained in
JPasswordField.
38
Program to create Login Page
Algorithm
1.Source of event
2.Event class
3.Event Listener
4.Create public class & implement Listener
5.Design GUI
6.Register the event
7.Call listener method
8.Read values of text field & display it
39
Thought of a Day
40
Java Adapter Classes
Drawback of Delegation Event Model
-Writing of every method of Listener is compulsory
-Example- MouseListener has 5 methods mouseClicked(),
mousePressed(),
mouseEntered(),mouseReleased(),mouseExited()
-We may need only one method then also we have to
write all other 4 methods
-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 are found in java.awt.event
41
Adapter classes
Adapter class Listener interface
WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
42
Mouse as a source with Adapter Classes
Algorithm:
1.Select the source of event
43
Mouse as a source with Adapter Classes
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter{
JFrame f;
MouseAdapterExample(){
f=new JFrame("Mouse Adapter");
f.addMouseListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public void mouseClicked(MouseEvent e) {
Graphics g=f.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
public static void main(String[] args) {
new MouseAdapterExample(); } }
44
Output
45
Key as a source with Adapter Classes
Algorithm:
1.Select the source of event
46
Key as a source of event using Adapter class
import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class KeyAdapterExample extends KeyAdapter{
JLabel l;
JTextArea area;
JFrame f;
KeyAdapterExample(){
f=new JFrame("Key Adapter");
l=new JLabel();
l.setBounds(20,50,200,20);
area=new JTextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
f.add(l);f.add(area);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void keyReleased(KeyEvent e) {
String text=area.getText();
String words[]=text.split("\\s");
l.setText("Words: "+words.length+" Characters:"+text.length());
}
public static void main(String[] args) {
new KeyAdapterExample(); } }
47
Thought of a Day
Java inner class
Type Description
50
Java Member inner class
Syntax:
class Outer{
//code
class Inner{
//code
}
}
51
Example of Member Inner class
52
Anonymous Inner class
• A class that have no name is known as
anonymous inner class in java.
53
Anonyms Inner Classes
import java.applet.*;
import java.awt.event.*;
/*
<applet code="InnerClassDemo" width=200 height=100>
</applet>
*/
public class InnerClassDemo extends Applet {
public void init() {
addMouseListener(new MyMouseAdapter());//Anonyms Inner class
public void mousePressed(MouseEvent me)
{
showStatus("Mouse Pressed");
}
}
54
Java Local inner class
55
Java local inner class example
public class localInner1{
private int data=30;//instance variable
void display()
{
class Local{ //class declared inside method
void msg(){System.out.println(data);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
localInner1 obj=new localInner1();
obj.display();
} }
Output=30 56
57
Thought of a day
Thought of a Day
59