Event handling in java
Download
1 / 11

Event Handling in Java - PowerPoint PPT Presentation


  • 105 Views
  • Uploaded on

Event Handling in Java. CSE 470 - Software Engineering Spring 2000 By Prasad. Introduction to Java. Java, platform independent language Two types of programs in Java Applets (embedded in web pages) Command line programs

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Event Handling in Java' - maya-glass


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Event handling in java

Event Handling in Java

CSE 470 - Software Engineering

Spring 2000

By Prasad


Introduction to java
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


To compile and run java applets
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


Running java applets continued
Running Java applets continued ..

<body>

<html>

<applet code = “programfile.class” width=200 height=200>

</applet>

</html>

</body>


Event model of java
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


Event listeners
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 ) { ……}

    }


Event listeners contd
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) { …..}

}


Event object
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


Event registration
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


Example simple action event
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");

}

}

}

}


References
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


ad