Event Handling in Java
130 likes | 332 Views
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
Event Handling in Java
E N D
Presentation Transcript
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 • We are primarily interested in developing GUIs in Java and in Event Handling mechanisms of Java
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 .. <body> <html> <applet code = “programfile.class” width=200 height=200> </applet> </html> </body>
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 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 …. 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 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 …. • 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 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 • 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