Event%20Handling%20in%20Java - PowerPoint PPT Presentation

About This Presentation
Title:

Event%20Handling%20in%20Java

Description:

Two types of programs in Java. Applets (embedded in web ... We are primarily interested in developing GUIs in Java and in Event Handling mechanisms of Java ... – PowerPoint PPT presentation

Number of Views:527
Avg rating:3.0/5.0
Slides: 12
Provided by: lauraca7
Learn more at: http://www.cse.msu.edu
Category:

less

Transcript and Presenter's Notes

Title: Event%20Handling%20in%20Java


1
Event Handling in Java
  • CSE 470 - Software Engineering
  • Spring 2000
  • By Prasad

2
Introduction to Java ..
  • Java, platform independent language
  • Two types of programs in Java
  • Applets (embedded in web pages)
  • Command line programs
  • We are primarily interested in developing GUIs in
    Java and in Event Handling mechanisms of Java

3
To Compile and run Java Applets
  • Include path
  • setenv PATH PATH/usr/java1.2/bin in your
    .personal file
  • To compile Java programs
  • javac programfile.java
  • To run Java applets
  • appletviewer programfile.html
  • A sample programfile.html is given in next slide

4
Running Java applets continued ..
  • ltbodygt
  • lthtmlgt
  • ltapplet code programfile.class width200
    height200gt
  • lt/appletgt
  • lt/htmlgt
  • lt/bodygt

5
Event Model of Java
  • Java uses delegation event Model

Register with source
Event Listener
Event Source
Event Object e
An object of a class that will implement methods
which will respond to events
Ex Button, Canvas, Text Box
6
Event Listeners
  • Event Listener Object has to implement a event
    listener interface.
  • Examples of Event Listener interfaces
  • Action Listener Interface
  • Mouse Listener Interface
  • Mouse Motion Listener Interface
  • Examples
  • Class mylistener implements ActionListener
  • / The following procedure is called when a
    Button is
  • pressed. /
  • public void actionPerformed ( ActionEvent e )

7
Event Listeners contd .
  • Class mylistener implements MouseListener
  • / Called when the mouse button is clicked /
  • public void mouseClicked(MouseEvent evt)
    .
  • / Called when the mouse button is
    depressed /
  • public void mousePressed(MouseEvent evt)
    ..
  • / Called when the mouse button is released /
  • public void mouseReleased(MouseEvent evt)
    ..

8
Event Object
  • Event Objects carry information about the event.
  • Event Listeners get information from Event
    sources thru Event Objects.
  • Examples
  • MouseEvent Evt
  • Evt.getX() --- gets the X-coordinate of mouse
    click.
  • Evt.getY() --- gets the Y-coordinate of mouse
    click
  • Evt.getSource() --- gets the source object of
    the mouse
  • click event like a button

9
Event Registration .
  • A Listener Object needs to register itself with
    an Event Source to be able to catch events on it.
  • Example
  • Button1.addActionListener( new mylistener1() )
  • Button1.addMouseListener( new mylistener2() )
  • Multiple Listeners can listen to a single event
    source
  • A single Listener can listen to multiple event
    sources

10
Example -- Simple Action Event
  • public class SimpleEvent extends Applet
  • Button button1 new Button("Press me")
  • public void init()
  • this.add(button1)
  • button1.addActionListener(new mylistener())
  • Class mylistener implements ActionListener
  • public void actionPerformed(ActionEvent e)
  • if(e.getSource() button1)
  • button1.setLabel("Again")

11
References
  • G. Cornell and C. Horstmann, Core Java, The
    Sunsoft Press Java Series, Second Edition, 1997
  • D. Joshi, L. Lemay, and C. Perkins, Teach
    yourself Java in Café in 21 days, Sams.net
    publishing, 1996
  • The Java Tutorial
  • http//java.sun.com/docs/books/tutorial/index.htm
  • D. Geary and A. McClellan, Graphic Java, The
    Sunsoft Press Java Series, 1997
Write a Comment
User Comments (0)
About PowerShow.com