1 / 11

Event Handling in Java

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

maya-glass
Download Presentation

Event Handling in Java

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 .. <body> <html> <applet code = “programfile.class” width=200 height=200> </applet> </html> </body>

  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

More Related