Event Handling - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

Event Handling

Description:

This is the Hard Part. import java.awt.*; import java.awt.event. ... Whenever either Button is pressed, actionPerformed gets called ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 55
Provided by: jeffch8
Category:

less

Transcript and Presenter's Notes

Title: Event Handling


1
Event Handling
  • My Buttons Dont Work!

2
Overview
  • Events
  • Listeners and Their Behaviors
  • The 4-step plan
  • import
  • implements
  • Override method(s)
  • Add Listener to the component

3
Events
  • 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

4
Intro 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)

5
ActionListener (easy)
  • Only one method to worry about!
  • Method
  • public void actionPerformed (ActionEvent e)
  • This method gets called for any button or
    textfield

6
MouseListener
  • 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)

7
MouseMotionListener
  • Methods
  • public void mouseMoved (MouseEvent e)
  • public void mouseDragged (MouseEvent e)

8
KeyListener
  • Methods
  • public void keyPressed (KeyEvent e)
  • public void keyReleased (KeyEvent e)
  • public void keyTyped (KeyEvent e)

9
The 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

10
Example
  • import java.awt.
  • public class Push extends java.applet.Applet
  • Button b
  • public void init ( )
  • b new Button (Press Me)
  • add ( b )

11
import
  • 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 )

12
Implement 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 )

13
Override 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)

14
Add 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)

15
This 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)

16
Syntax
  • b.addActionListener (this)

The Button
17
Syntax
  • b.addActionListener (this)

The button is having someone listen to it when it
is clicked
18
Syntax
  • 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

19
How it works
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
20
The User Clicks the Button
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
21
An ActionEvent Object is Created
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
22
actionPerformed is Called
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
23
actionPerformed is Called
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
24
showStatus
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
25
actionPerformed is Finished
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
26
What about B2?
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
27
B2 pressed(an actionEvent generated again)
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
28
actionPerformed Called(again)
ActionEvent
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
29
showStatus (again)
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
30
actionPerformed Finished
public void actionPerformed (ActionEvent e)
showStatus (A button was pressed)
31
Problem
  • 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!

32
Better Version
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
33
B2 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)
34
actionPeformed 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)
35
Get 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)
36
Get 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
37
Check 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)
38
Check 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
39
showStatus
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
40
actionPeformed Finished
public void actionPerformed (ActionEvent e)
if (e.getSource( ) b1) showStatus (B1 was
pressed) else if (e.getSource( )
b2) showStatus (B2 was pressed)
41
MouseEvents
  • Two different interfaces
  • MouseListener
  • mousePressed
  • mouseReleased
  • mouseClicked
  • mouseEntered
  • mouseExited
  • MouseMotionListener
  • mouseMoved
  • mouseDragged

42
MouseMotionListener Example(Start with applet
Skeleton and init)
  • import java.awt.
  • public class MouseTest extends java.applet.Applet
  • public void init ( )
  • //end class

43
import
  • import java.awt.
  • import java.awt.event.
  • public class MouseTest extends java.applet.Applet
  • public void init ( )
  • //end class

44
Implement the Correct Interface
  • import java.awt.
  • import java.awt.event.
  • public class MouseTest extends java.applet.Applet
  • implements MouseMotionListener
  • public void init ( )
  • //end class

45
Override 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

46
Add 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

47
Syntax
  • this.addMouseMotionListener (this)

48
Syntax
  • this.addMouseMotionListener (this)

The Applet
49
Syntax
  • this.addMouseMotionListener (this)

The applet is adding someone to listen to it
when the mouse is moved
50
Syntax
  • 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!

51
Example
MouseEvent
public void mouseMoved (MouseEvent e)
showStatus (e.getX( ) e.getY())
52
MouseEvents
  • 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 ?

53
Summary
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com