Title: Event Handling
1Event Handling
2Overview
- Events
- Listeners and Their Behaviors
- The 4-step plan
- import
- implements
- Override method(s)
- Add Listener to the component
3Events
- Events occur all the time
- Mouse movement
- Buttons pressed
- Windows resized
- Keys typed
- Event-Handling Idea have specialized methods to
handle events when the events occur
4Intro to Interfaces
- When you implement an interface, you must
override methods you inherit - Compiler will let you know which methods you need
to override when you compile - Four common interfaces
- ActionListener (for Buttons and TextFields)
- MouseListener (for mouse clicks an stuff)
- MouseMotionListener (movement and dragging)
- KeyListener (for keyboard events)
5ActionListener (easy)
- Only one method to worry about!
- Method
- public void actionPerformed (ActionEvent e)
- This method gets called for any button or
textfield
6MouseListener
- Methods
- public void mousePressed (MouseEvent e)
- public void mouseReleased (MouseEvent e)
- public void mouseClicked (MouseEvent e)
- public void mouseEntered (MouseEvent e)
- public void mouseExited (MouseEvent e)
7MouseMotionListener
- Methods
- public void mouseMoved (MouseEvent e)
- public void mouseDragged (MouseEvent e)
8KeyListener
- Methods
- public void keyPressed (KeyEvent e)
- public void keyReleased (KeyEvent e)
- public void keyTyped (KeyEvent e)
9The 4-Step Plan
- import java.awt.event.
- Implement the correct interface
- Override the methods you get with that interface
- Add the listener to the component
10Example
- import java.awt.
- public class Push extends java.applet.Applet
-
- Button b
- public void init ( )
- b new Button (Press Me)
-
- add ( b )
-
-
-
-
11import
- import java.awt.
- import java.awt.event.
- public class Push extends java.applet.Applet
-
- Button b
- public void init ( )
- b new Button (Press Me)
-
- add ( b )
-
-
-
-
12Implement the Correct Interface
- import java.awt.
- import java.awt.event.
- public class Push extends java.applet.Applet
- implements ActionListener
- Button b
- public void init ( )
- b new Button (Press Me)
-
- add ( b )
-
-
-
-
13Override the Correct Methods
- import java.awt.
- import java.awt.event.
- public class Push extends java.applet.Applet
- implements ActionListener
- Button b
- public void init ( )
- b new Button (Press Me)
-
- add ( b )
-
- public void actionPerformed (ActionEvent e)
- b.setBackground (Color.blue)
-
14Add the Listener
- import java.awt.
- import java.awt.event.
- public class Push extends java.applet.Applet
- implements ActionListener
- Button b
- public void init ( )
- b new Button (Press Me)
- b.addActionListener (this)
- add ( b )
-
- public void actionPerformed (ActionEvent e)
- b.setBackground (Color.blue)
-
15This is the Hard Part
- import java.awt.
- import java.awt.event.
- public class Push extends java.applet.Applet
- implements ActionListener
- Button b
- public void init ( )
- b new Button (Press Me)
- b.addActionListener (this)
- add ( b )
-
- public void actionPerformed (ActionEvent e)
- b.setBackground (Color.blue)
-
16Syntax
- b.addActionListener (this)
The Button
17Syntax
- b.addActionListener (this)
The button is having someone listen to it when it
is clicked
18Syntax
- b.addActionListener (this)
- this means the class that were inside (the
applet) - Passing the applet itself as a parameter
- The applet is the one who implemented the
- ActionListener interface
- The applet is the one that has the method
- actionPerformed (ActionEvent e)
- The applet will be the one to handle the events
- that the button generates
19How it works
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
20The User Clicks the Button
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
21An ActionEvent Object is Created
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
22actionPerformed is Called
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
23actionPerformed is Called
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
24showStatus
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
25actionPerformed is Finished
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
26What about B2?
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
27B2 pressed(an actionEvent generated again)
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
28actionPerformed Called(again)
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
29showStatus (again)
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
30actionPerformed Finished
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
31Problem
- We have 2 Buttons
- Whenever either Button is pressed,
actionPerformed gets called - Need a way to determine which Button was pressed
- Ask the ActionEvent that was generated!
32Better Version
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
33B2 pushed
ActionEvent from b2
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
34actionPeformed Called
ActionEvent from b2
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
35Get the Source of the ActionEvent
ActionEvent from b2
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
36Get the Source of the ActionEvent
ActionEvent from b2
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
false
37Check again
ActionEvent from b2
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
38Check again
ActionEvent from b2
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
true
39showStatus
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
40actionPeformed Finished
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
41MouseEvents
- Two different interfaces
- MouseListener
- mousePressed
- mouseReleased
- mouseClicked
- mouseEntered
- mouseExited
- MouseMotionListener
- mouseMoved
- mouseDragged
42MouseMotionListener Example(Start with applet
Skeleton and init)
- import java.awt.
- public class MouseTest extends java.applet.Applet
-
- public void init ( )
-
-
-
-
-
-
- //end class
43import
- import java.awt.
- import java.awt.event.
- public class MouseTest extends java.applet.Applet
-
- public void init ( )
-
-
-
-
-
-
- //end class
44Implement the Correct Interface
- import java.awt.
- import java.awt.event.
- public class MouseTest extends java.applet.Applet
- implements MouseMotionListener
- public void init ( )
-
-
-
-
-
-
- //end class
45Override the Correct Methods
- import java.awt.
- import java.awt.event.
- public class MouseTest extends java.applet.Applet
- implements MouseMotionListener
- public void init ( )
-
-
- public void mouseMoved (MouseEvent e)
- showStatus (e.getX( ) e.getY( ) )
-
- public void mouseDragged (MouseEvent e)
- //end class
46Add the Listener
- import java.awt.
- import java.awt.event.
- public class MouseTest extends java.applet.Applet
- implements MouseMotionListener
- public void init ( )
- this.addMouseMotionListener (this)
-
- public void mouseMoved (MouseEvent e)
- showStatus (e.getX( ) e.getY( ) )
-
- public void mouseDragged (MouseEvent e)
- //end class
47Syntax
- this.addMouseMotionListener (this)
48Syntax
- this.addMouseMotionListener (this)
The Applet
49Syntax
- this.addMouseMotionListener (this)
The applet is adding someone to listen to it
when the mouse is moved
50Syntax
- this.addMouseMotionListener (this)
- In this case, the applet is the one that
implemented - the MouseMotionListener interface and has the
correct - methods in it
- Applet is both generating MouseEvents and
handling - them!
51Example
MouseEvent
public void mouseMoved (MouseEvent e)
showStatus (e.getX( ) e.getY())
52MouseEvents
- These can occur several hundred times in the
applets lifetime - Ask the MouseEvent its x and y position on the
screen - Can also getClickCount( ) to determine
- Single click
- Double click
- Triple click
- Octuple click ?
53Summary
- Applets can listen for events
- 4 things
- import
- Implement correct interface
- Override methods
- Add listener to the component (applet or button)
- Interfaces
- ActionListener (buttons and textfields)
- MouseListener (clicks and focus)
- MouseMotionListener (mouse movement)
54(No Transcript)