Event Handlers - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Event Handlers

Description:

The actionPerformed method runs based on a certain specified asynchronous event ... Asynchronous. On NO schedule. Happens without warning. Objects of this ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 31
Provided by: cseBu
Category:

less

Transcript and Presenter's Notes

Title: Event Handlers


1
Event Handlers
  • called Action Listeners

2
JButtons
An Event Java code that Listens for an
event Is called an Action Listener
3
3 new classes
  • Use these two
  • ActionListener a private inner class
  • ActionEvent a helper class used by
    ActionListener
  • To write your own
  • Any class with actionPerformed( ) method

4
Add this to any class
  • public class EventClass extends JFrame implements
    ActionListener
  • public void actionPerformed ( ActionEvent e )
  • System.out.println(Something just
    happened)
  • System.out.println( e.getActionCommand( )
    )

Helper class
5
What does implement mean
  • Called an Interface
  • Just like extend, only different.
  • In extend, you can choose to use as-is, or
    re-write the methods of the parent class.
  • The author of a class that is implemented,
    insists that YOU write certain pre-determined
    methods for it.

6
Why implement an Interface?
  • Because there are probably tons of classes that
    USE the implemented class, and if youre writing
    a new one, it must fit exactly as previous ones
    did
  • Heres the problem how else would you write a
    class that comfortably slips into an application
    thats already running?

7
ActionListener class
  • An interface written by someone else, and you
    must adhere to the contract
  • Class that implements the ActionListener must
    meet one obligation
  • provide a method called actionPerformed that
    receives an ActionEvent object, as a means of
    telling what just happened?.
  • The actionPerformed method runs based on a
    certain specified asynchronous event

8
Add this to any code
  • public class eventClass extends JFrame implements
    ActionListener
  • public void actionPerformed ( ActionEvent e )
  • System.out.println(Something just
    happened)
  • System.out.println( e.getActionCommand( )
    )

9
ActionEvent objects
  • An object of the helper ActionEvent class has the
    following property
  • It gets notified of computer events.
  • It is an object of a class that contains methods
    that deal with events
  • e.getActionCommand()
  • e.getSource()
  • e.toString()

10
Asynchronous
  • On NO schedule
  • Happens without warning

11
Objects of this implemented class
  • Useful when this becomes an object somewhere in
    the immediate code (for instance, a class
    constructor)
  • eventClass handler new eventClass( )

12
the JButton
  • grabs one ActionListener object as its own
  • (e.g. handler)
  • sends its activity to that ActionListener

13
Tying a component to an ActionListener
  • Components JButtons, JComboBoxes, JTextFields,
    and other GUI controls.
  • JButton helloButton new JButton( Hello)
  • // the class that contains the action listener
  • eventClass handler new eventClass( )
  • helloButton.addActionListener( handler )
  • // or if were already IN the class
  • helloButton.addActionListener( this )

Has to be an object that contains an
actionPerformed method
14
  • Watch the demo

15
e.getSource( )
  • Pull Jbuttons and other JComponents out of
    constructor brackets.
  • Make them public and known to every one.
  • Can be tested in if statements

16
test in actionPerformed
  • public void actionPerformed( ActionEvent e )
  • System.out.println(e.getActionCommand())
  • if (e.getSource( ) exitButton)
  • System.out.println("Exiting")
  • System.exit(0)
  • // end actionPerformed method

17
Handlers in General
18
A Component
  • JButton relies on user action
  • an event producer
  • JButton helloButton new JButton( Hello)

19
Registration
  • Tying a handler object to an event producer
  • helloButton.addActionListener( myHandler )
  • helloButton.addActionListener( this )
  • if the current class contains the
    actionPerformed method( )

20
A Handler Object
  • Instantiate the Private, Inner Class
  • JButton exitButton new JButton( "Exit" )
  • add( exitButton )
  • eventClass handler new eventClass( )
  • exitButton.addActionListener( handler )
  • handler is an object
  • Can be attached to a component
  • exitButton.addActionListener( this )

21
A Timer Event
  • Imagine a clock ticking at every tick, the
    computer generates an event.
  • Computers have many internal clocks.
  • Some with nano-second timing.
  • 10-9
  • A nanosecond is to a second as a second is to 30
    years
  • The standard measure of computer time is the
    millisecond - 10-3

22
Time
  • second
  • millisecond - 1/1000 - 10-3
  • microsecond - 1/1,000,000 - 10-6
  • nanosecond - 1/1,000,000,000 - 10-9

23
The Timer Component
  • Public Timer tickTimer
  • tickTimer new Timer( 1000, null )
  • tickTimer.addActionListener( this )
  • tickTimer.start()
  • tickTimer.stop()

How many milliseconds between events
what code runs every tick
24
(No Transcript)
25
Animation Coding Example
26
Main Class
  • import javax.swing.
  • import java.awt.
  • import java.awt.event.
  • public class bouncer extends JFramepublic int
    ballX 0public int ballY 0public int
    diameter 50public int width 400public int
    height 400public int counter 0public
    Timer tickTimer new Timer(10, null)

27
constructor
  • public bouncer() setSize( width, height
    ) setVisible( true ) setDefaultCloseOper
    ation( EXIT_ON_CLOSE )
    tickTimer.addActionListener( this )
    tickTimer.start( )

28
paint method
  • public void paint (Graphics g)
  • // background
  • g.setColor( Color.white )
  • g.fillRect( 0,0, width, height)
  • // ball
  • g.setColor( Color.red )
  • g.fillOval( ballX, ballY, diameter, diameter
    )

29
actionPerformed every time the timer ticks
  • public void actionPerformed(ActionEvent e)
  • ballY
  • ballX
  • repaint( ) // like calling the paint method
  • if ( counter 400 ) // inc. and test
    together
  • tickTimer.stop( )

30
add a bounce every time the timer goes off
  • public void actionPerformed( ActionEvent e )
  • ballY
  • ballX
  • repaint( )
  • if (counter 400)
  • tickTimer.stop()
  • if (counter gt 200)
  • ballY ballY - 2 // net effect -1
Write a Comment
User Comments (0)
About PowerShow.com