Event handlinghttp://improvejava.blogspot.in/                                  1
ObjectivesOn completion of this period, you would beable to know• Using delegation event model• Handling keyboard events• ...
RecapIn the previous class, you have leant• Event handling• Key event handling                http://improvejava.blogspot....
Using the Delegation Event Model•   Now that you have learned the theory behind    the delegation event model and have had...
Handling Keyboard Events• To handle keyboard events, you must implement  the KeyListener interface• This interface defines...
Example Program• The following program demonstrates keyboard  input• It echoes keystrokes to the applet window and  shows ...
Example Program// Demonstrate the key event handlersimport java.awt.*;import java.awt.event.*;import java.applet.*;/*<appl...
Example Program                         contd..    public void keyPressed(KeyEvent    ke) {          showStatus("Key Down"...
Example Program                         contd..• Sample output is shown here             Fig 63.1 Output of the program   ...
Handling Mouse Events• To handle mouse events, you have to implement  mouse related listeners• There are 3 listeners relat...
Handling Mouse Events                         contd..• In the next example program we implement• This interface defines fi...
Example Program• It displays the current coordinates of the mouse  in the applet’s status window  – Each time a button is ...
Example Program// Demonstrate the mouse event handlersimport java.awt.*;import java.awt.event.*;import java.applet.*;/*<ap...
Example Program                              contd..// Handle mouse enteredpublic void mouseEntered(MouseEvent me) {// sav...
Example Program                               contd..// Handle button pressed.public void mousePressed(MouseEvent me) {// ...
Example Program                               contd..    // Handle mouse clicked.    public void mouseClicked(MouseEvent m...
Example Program                           contd..• Sample output from this program is shown here:               Fig 63.2  ...
Adapter Classes• Java provides a special feature, called an  adapter class, that can simplify the creation of  event handl...
Adapter Classes                              contd..• The following are the list of Adapter classes Adapter Class         ...
Example Program// Demonstrate an adapter.import java.awt.*;import java.awt.event.*;import java.applet.*;/*<applet code="Ad...
Example Program                            contd..class MyMouseAdapter extends MouseAdapter {   AdapterDemo adapterDemo;  ...
Example Program                               contd..       // Handle mouse clicked.       public void mouseClicked(MouseE...
Example Program• As you can see by looking at the program  – Not all methods defined the MouseMotionListener and    MouseL...
SummaryWe have discussed in this class  –   Using delegation event model  –   Handling keyboard events  –   Handling Mouse...
Quiz1. Which class is not involved in delegation model?   a) Event class   b) Event listener   c) Event source   d) Thread...
Frequently Asked Questions1. Describe how to use delegation model for event  handling                 http://improvejava.b...
Upcoming SlideShare
Loading in …5
×

Event handling63

1,054 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,054
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
36
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Event handling63

  1. 1. Event handlinghttp://improvejava.blogspot.in/ 1
  2. 2. ObjectivesOn completion of this period, you would beable to know• Using delegation event model• Handling keyboard events• Handling Mouse events• Adapter class http://improvejava.blogspot.in/ 2
  3. 3. RecapIn the previous class, you have leant• Event handling• Key event handling http://improvejava.blogspot.in/ 3
  4. 4. 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 http://improvejava.blogspot.in/ 4
  5. 5. 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) http://improvejava.blogspot.in/ 5
  6. 6. 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 http://improvejava.blogspot.in/ 6
  7. 7. Example Program// 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 coordinates public void init() { addKeyListener(this); requestFocus(); // request input focus} http://improvejava.blogspot.in/ 7
  8. 8. 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); }} http://improvejava.blogspot.in/ 8
  9. 9. Example Program contd..• Sample output is shown here Fig 63.1 Output of the program http://improvejava.blogspot.in/ 9
  10. 10. Handling Mouse Events• To handle mouse events, you have to implement mouse related listeners• There are 3 listeners related to mouse – MouseListener – MouseMotionListener – MouseWheelListener http://improvejava.blogspot.in/ 10
  11. 11. Handling Mouse Events contd..• 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 http://improvejava.blogspot.in/ 11
  12. 12. Example Program• 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 http://improvejava.blogspot.in/ 12
  13. 13. 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 mouse public void init() { addMouseListener(this); } http://improvejava.blogspot.in/ 13
  14. 14. Example Program contd..// Handle mouse enteredpublic void mouseEntered(MouseEvent me) {// save coordinates mouseX = 0; mouseY = 10; msg = "Mouse entered."; repaint();}// Handle mouse exited.public void mouseExited(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse exited."; repaint();} http://improvejava.blogspot.in/ 14
  15. 15. Example Program contd..// Handle button pressed.public void mousePressed(MouseEvent me) {// save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint();}// Handle button released.public void mouseReleased(MouseEvent me) {// save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint();} http://improvejava.blogspot.in/ 15
  16. 16. Example Program contd.. // Handle mouse clicked. public void mouseClicked(MouseEvent me) { // save coordinates mouseX = 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); }} http://improvejava.blogspot.in/ 16
  17. 17. Example Program contd..• Sample output from this program is shown here: Fig 63.2 Output of the program http://improvejava.blogspot.in/ 17
  18. 18. 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 http://improvejava.blogspot.in/ 18
  19. 19. Adapter Classes contd..• 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 http://improvejava.blogspot.in/ 19
  20. 20. 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)); }} http://improvejava.blogspot.in/ 20
  21. 21. Example Program contd..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"); }} http://improvejava.blogspot.in/ 21
  22. 22. Example Program contd.. // 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"); }} http://improvejava.blogspot.in/ 22
  23. 23. Example Program• 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 http://improvejava.blogspot.in/ 23
  24. 24. SummaryWe have discussed in this class – Using delegation event model – Handling keyboard events – Handling Mouse events – Adapter class http://improvejava.blogspot.in/ 24
  25. 25. Quiz1. Which class is not involved in delegation model? a) Event class b) Event listener c) Event source d) Thread class http://improvejava.blogspot.in/ 25
  26. 26. Frequently Asked Questions1. Describe how to use delegation model for event handling http://improvejava.blogspot.in/ 26

×