File 1707213348 0003311 4 Eventhandling Updated
File 1707213348 0003311 4 Eventhandling Updated
1
What is an Event?
Change in the state of an object is known as event
i.e. event describes the change in state of source.
Events are generated as result of user interaction
with the graphical user interface components. For
example, clicking on a button, moving the mouse,
entering a character through keyboard,selecting
an item from list, scrolling the page are the
activities that causes an event to happen.
2
Types of Event
The events can be broadly classified into two categories:
Foreground Events - Those events which require the direct
interaction of user. They are generated as consequences of a
person interacting with the graphical components in Graphical
User Interface. For example, clicking on a button, moving the
mouse, entering a character through keyboard, selecting an
item from list, scrolling the page etc.
4
Steps involved in event handling
The User clicks the button and the event is
generated.
Now the object of concerned event class is
created automatically and information about the
source and the event get populated with in same
object.
Event object is forwarded to the method of
registered listener class.
the method is now get executed and returns
5
Event and Listener (Java Event
Handling)
6
Java Event classes and Listener
interfaces
7
Steps to perform Event Handling
8
Registration Methods
For registering the component with the Listener, many classes provide the registration methods.
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a) 9
Java Event Handling Code
Within class
Other class
Anonymous class
10
Java event handling by implementing ActionListener
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
13
Java event handling by anonymous class
import java.awt.*;
import java.awt.event.*;
class AEvent3 extends Frame{
TextField tf;
AEvent3(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(50,120,80,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(){
tf.setText("hello");
}
});
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent3();
Java ActionListener Interface
15
Java KeyListener Interface
l=new Label();
l.setBounds(20,50,100,20);
area=new TextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
l.setText("Key Pressed");
}
public void keyReleased(KeyEvent e) {
l.setText("Key Released");
}
public void keyTyped(KeyEvent e) {
l.setText("Key Typed");
}
l=new Label();
l.setBounds(20,50,200,20);
area=new TextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
String text=area.getText();
String words[]=text.split("\\s");
l.setText("Words: "+words.length+" Characters:"+text.length());
}
public void keyTyped(KeyEvent e) {}
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
} 24
}
Java MouseListener Example 2
import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample2 extends Frame implements MouseListener{
MouseListenerExample2(){
addMouseListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
26
Java MouseMotionListener Example
import java.awt.*;
import java.awt.event.*;
public class MouseMotionListenerExample extends Frame implements MouseMotionListener{
MouseMotionListenerExample(){
addMouseMotionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e) {}
28
Java WindowListener Example
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowExample extends Frame implements WindowListener{
WindowExample(){
addWindowListener(this);
setSize(400,400);
setLayout(null);
setVisible(true);
}
31
javax.swing.event Adapter classes
32
Java WindowAdapter Example
import java.awt.*;
import java.awt.event.*;
public class AdapterExample{
Frame f;
AdapterExample(){
f=new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
f.dispose();
}
});
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new AdapterExample();
}
}
33
Java MouseAdapter Example
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter{
Frame f;
MouseAdapterExample(){
f=new Frame("Mouse Adapter");
f.addMouseListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
Graphics g=f.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
f.add(l);f.add(area);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void keyReleased(KeyEvent e) {
String text=area.getText();
String words[]=text.split("\\s");
l.setText("Words: "+words.length+" Characters:"+text.length());
}
Drawback of Applet
Plugin is required at client browser to execute applet.
36
An applet is a Java program that runs in a Web browser. An applet can be a fully
functional Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java
application, including the following −
An applet is a Java class that extends the java.applet.Applet class.
A main() method is not invoked on an applet, and an applet class will not define
main().
Applets are designed to be embedded within an HTML page.
When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
A JVM is required to view an applet. The JVM can be either a plug-in of the Web
browser or a separate runtime environment.
The JVM on the user's machine creates an instance of the applet class and invokes
various methods during the applet's lifetime.
37
Hierarchy of Applet
38
Lifecycle of Java Applet
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
39
Lifecycle methods for Applet:
40
Four methods in the Applet class gives you the framework on which you
build any serious applet −
init − This method is intended for whatever initialization is needed for your
applet. It is called after the param tags inside the applet tag have been
processed.
start − This method is automatically called after the browser calls the init
method. It is also called whenever the user returns to the page containing
the applet after having gone off to other pages.
stop − This method is automatically called when the user moves off the
page on which the applet sits. It can, therefore, be called repeatedly in the
same applet.
destroy − This method is only called when the browser shuts down
normally. Because applets are meant to live on an HTML page, you should
not normally leave resources behind after a user leaves the page that
contains the applet.
paint − Invoked immediately after the start() method, and also any time the
applet needs to repaint itself in the browser. The paint() method is actually
inherited from the java.awt.
41
java.awt.Component class
42
How to run an Applet?
By html file.
By appletViewer tool (for testing purpose).
43
Simple example of Applet by html file:
To execute the applet by html file, create an applet and
compile it. After that create an html file and place the applet
code in html file. Now click the html file.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
} 44
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height
="300">
</applet>
</body>
</html>
45
Simple example of Applet by appletviewer
tool:
To execute the applet by appletviewer tool, create
an applet that contains applet tag in comment and
compile it. After that run it by: appletviewer
First.java. Now Html file is not required but it is
for testing purpose only.
To execute the applet by appletviewer tool, write
in command prompt:
c:\>javac First.java
c:\>appletviewer First.java
46
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/ 47
Displaying Image in Applet
Applet is mostly used in games and animation. For this purpose image is
required to be displayed. The java.awt.Graphics class provide a method
drawImage() to display the image.
Syntax of drawImage() method:
public abstract boolean drawImage(Image img, int x, int y,
ImageObserver observer): is used draw the specified image.
48
import java.awt.*;
import java.applet.*;
Image picture;
}
49
<html>
<body>
<applet code="DisplayImage.class" width="300"
height="300">
</applet>
</body>
</html>
50
EventHandling in Applet
51
Example of EventHandling in applet:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;
b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
}
<html>
<body>
<applet code="EventApplet.class" width="300
" height="300">
</applet>
</body>
</html>
53