Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Event Handling in Java CSE 470 - Software Engineering Spring 2000 By Prasad."— Presentation transcript:

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

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

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

4 Running Java applets continued..

5 Event Model of Java n Java uses delegation event Model Event Source Event Listener Ex: Button, Canvas, Text Box An object of a class that will implement methods which will respond to events Register with source Event Object e

6 Event Listeners … n Event Listener Object has to implement a event listener interface. n Examples of Event Listener interfaces: Action Listener Interface Mouse Listener Interface Mouse Motion Listener Interface n 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 n Event Objects carry information about the event. Event Listeners get information from Event sources thru Event Objects. n 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 …. n A Listener Object needs to register itself with an Event Source to be able to catch events on it. n Example: Button1.addActionListener( new mylistener1() ) Button1.addMouseListener( new mylistener2() ) n Multiple Listeners can listen to a single event source n 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 n G. Cornell and C. Horstmann, “Core Java”, The Sunsoft Press Java Series, Second Edition, 1997 n D. Joshi, L. Lemay, and C. Perkins, “Teach yourself Java in Café in 21 days”, Sams.net publishing, 1996 n The Java Tutorial http://java.sun.com/docs/books/tutorial/index.htm n D. Geary and A. McClellan, “Graphic Java”, The Sunsoft Press Java Series, 1997


Download ppt "Event Handling in Java CSE 470 - Software Engineering Spring 2000 By Prasad."

Similar presentations


Ads by Google