Event handling63
-
Upload
myrajendra -
Category
Documents
-
view
570 -
download
2
Transcript of Event handling63
![Page 1: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/1.jpg)
http://improvejava.blogspot.in/
Event handling
1
![Page 2: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/2.jpg)
http://improvejava.blogspot.in/
On completion of this period, you would be able to know• Using delegation event model• Handling keyboard events• Handling Mouse events• Adapter class
2
Objectives
![Page 3: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/3.jpg)
http://improvejava.blogspot.in/
Recap
3
In the previous class, you have leant
• Event handling
• Key event handling
![Page 4: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/4.jpg)
http://improvejava.blogspot.in/
Using the Delegation Event Model
• Now that you have learned the theory behind the delegation event model and have had
• an overview of its various components• To apply this model, follow these two steps:
1. Implement the appropriate interface in the listener so that it will receive the type of event desired
2. Implement code to register the listener as a recipient for the event notifications
4
![Page 5: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/5.jpg)
http://improvejava.blogspot.in/
Handling Keyboard Events
• To handle keyboard events, you must implement the KeyListener interface
• This interface defines three methods – keyPressed( ) – keyReleased( )– keyTyped( ) (when a character has been
entered)
5
![Page 6: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/6.jpg)
http://improvejava.blogspot.in/
Example Program
• The following program demonstrates keyboard input
• It echoes keystrokes to the applet window and shows the pressed/released status of each key in the status window
6
![Page 7: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/7.jpg)
http://improvejava.blogspot.in/
// Demonstrate the key event handlersimport java.awt.*;import java.awt.event.*;import java.applet.*;/*<applet code="SimpleKey" width=300 height=100></applet>*/public class SimpleKey extends Applet implements KeyListener {
String msg = "";int X = 10, Y = 20; // output coordinatespublic void init() {addKeyListener(this);requestFocus(); // request input focus
}
Example Program
7
![Page 8: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/8.jpg)
http://improvejava.blogspot.in/
Example Program contd..
public void keyPressed(KeyEvent ke) {
showStatus("Key Down");}public void keyReleased(KeyEvent ke) {
showStatus("Key Up");}public void keyTyped(KeyEvent ke) {
msg += ke.getKeyChar();repaint();
}// Display keystrokes.public void paint(Graphics g) {
g.drawString(msg, X, Y);}
}8
![Page 9: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/9.jpg)
http://improvejava.blogspot.in/
• Sample output is shown here
Example Program contd..
Fig 63.1 Output of the program
9
![Page 10: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/10.jpg)
http://improvejava.blogspot.in/
Handling Mouse Events
• To handle mouse events, you have to implement mouse related listeners
• There are 3 listeners related to mouse– MouseListener– MouseMotionListener– MouseWheelListener
10
![Page 11: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/11.jpg)
http://improvejava.blogspot.in/
• In the next example program we implement
• This interface defines five methods– If the mouse is pressed and released at the same
point, mouseClicked( ) is invoked– When the mouse enters a component, the
mouseEntered( ) method is called– When it leaves, mouseExited( ) is called – The mousePressed( ) and mouseReleased( )
methods are invoked when the mouse is pressed and released
Handling Mouse Events contd..
11
![Page 12: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/12.jpg)
http://improvejava.blogspot.in/
• It displays the current coordinates of the mouse in the applet’s status window– Each time a button is pressed, the word “Down” is
displayed at the location of the mouse pointer– Each time the button is released, the word “Up” is
shown– If a button is clicked, the message “Mouse clicked” is
displayed in the upper-left corner of the applet display area
– As the mouse enters or exits the applet window, a message is displayed in the upper-left corner of the applet display area
Example Program
12
![Page 13: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/13.jpg)
http://improvejava.blogspot.in/
Example Program
// Demonstrate the mouse event handlersimport java.awt.*;import java.awt.event.*;import java.applet.*;/*<applet code="MouseEvents" width=300 height=100></applet>*/public class MouseEvents extends Applet implements MouseListener {
String msg = "";int mouseX = 0, mouseY = 0; // coordinates of mousepublic void init() {
addMouseListener(this);}
13
![Page 14: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/14.jpg)
http://improvejava.blogspot.in/
// Handle mouse enteredpublic void mouseEntered(MouseEvent me) {// save coordinatesmouseX = 0;mouseY = 10;msg = "Mouse entered.";repaint();}// Handle mouse exited.public void mouseExited(MouseEvent me) {// save coordinatesmouseX = 0;mouseY = 10;msg = "Mouse exited.";repaint();}
Example Program contd..
14
![Page 15: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/15.jpg)
http://improvejava.blogspot.in/
// Handle button pressed.public void mousePressed(MouseEvent me) {// save coordinatesmouseX = me.getX();mouseY = me.getY();msg = "Down";repaint();}// Handle button released.public void mouseReleased(MouseEvent me) {// save coordinatesmouseX = me.getX();mouseY = me.getY();msg = "Up";repaint();}
Example Program contd..
15
![Page 16: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/16.jpg)
http://improvejava.blogspot.in/
// Handle mouse clicked.public void mouseClicked(MouseEvent me) {
// save coordinatesmouseX = 0;mouseY = 10;msg = "Mouse clicked.";repaint();
}// Display msg in applet window at current X,Y location.public void paint(Graphics g) {
g.drawString(msg, mouseX, mouseY);}
}
Example Program contd..
16
![Page 17: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/17.jpg)
http://improvejava.blogspot.in/
• Sample output from this program is shown here:
Example Program contd..
Fig 63.2 Output of the program
17
![Page 18: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/18.jpg)
http://improvejava.blogspot.in/
Adapter Classes
• Java provides a special feature, called an adapter class, that can simplify the creation of event handlers in certain situations
• An adapter class provides an empty implementation of all methods in an event listener interface
• Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface
18
![Page 19: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/19.jpg)
http://improvejava.blogspot.in/
• The following are the list of Adapter classes
Adapter Class Listener Interface
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
FocusAdapter FocusListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
WindowAdapter WindowListener
Adapter Classes contd..
19
![Page 20: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/20.jpg)
http://improvejava.blogspot.in/
Example Program
// Demonstrate an adapter.import java.awt.*;import java.awt.event.*;import java.applet.*;/*<applet code="AdapterDemo" width=300 height=100></applet>*/public class AdapterDemo extends Applet {
public void init() {addMouseListener(new MyMouseAdapter(this));addMouseMotionListener(new MyMouseMotionAdapter(this));}
}
20
![Page 21: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/21.jpg)
http://improvejava.blogspot.in/
class MyMouseAdapter extends MouseAdapter {AdapterDemo adapterDemo;public MyMouseAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;}// Handle mouse clicked.public void mouseClicked(MouseEvent me) {adapterDemo.showStatus("Mouse clicked");}
}
Example Program contd..
21
![Page 22: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/22.jpg)
http://improvejava.blogspot.in/
// Handle mouse clicked.public void mouseClicked(MouseEvent me) {adapterDemo.showStatus("Mouse clicked");
}}class MyMouseMotionAdapter extends MouseMotionAdapter {
AdapterDemo adapterDemo;public MyMouseMotionAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;}// Handle mouse dragged.public void mouseDragged(MouseEvent me) {
adapterDemo.showStatus("Mouse dragged");}
}
Example Program contd..
22
![Page 23: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/23.jpg)
http://improvejava.blogspot.in/
• As you can see by looking at the program– Not all methods defined the MouseMotionListener and
MouseListener interfaces are implemented– Saves you a considerable amount of effort and
prevents your code from becoming cluttered with empty methods
Example Program
23
![Page 24: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/24.jpg)
http://improvejava.blogspot.in/
Summary
We have discussed in this class– Using delegation event model– Handling keyboard events– Handling Mouse events– Adapter class
24
![Page 25: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/25.jpg)
http://improvejava.blogspot.in/
Quiz
1. Which class is not involved in delegation model?
a) Event class
b) Event listener
c) Event source
d) Thread class
25
![Page 26: Event handling63](https://reader031.fdocuments.us/reader031/viewer/2022020720/55561972d8b42ae0238b4ff8/html5/thumbnails/26.jpg)
http://improvejava.blogspot.in/
Frequently Asked Questions
1. Describe how to use delegation model for event handling
26