110 likes | 386 Views
Events in Java. CSE 470 - Software Engineering Fall 1999 Updated by J. Brown. Example 1-- Simple Action Event. import java.awt.*; Import java.awt.event.*; import java.applet.*; public class SimpleEvent extends Applet implements ActionListener {
E N D
Events in Java CSE 470 - Software Engineering Fall 1999 Updated by J. Brown
Example 1-- Simple Action Event import java.awt.*; Import java.awt.event.*; import java.applet.*; public class SimpleEvent extends Applet implements ActionListener { Button button1 = new Button("Press me"); public void init() { this.add(button1); button1.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == button1) { button1.setLabel("Again"); } } }
Handling Events • Events are first-class objects. • There is a class Event. • Subclasses identify different kinds of events. • Components fire events. • Other objects can listen for/act upon these events. • Interested objects register themselves as a Listener with the component that fires the event.
Event Routing • Lots of different kinds of events • Different events carry different types of information • E.g., ActionEvent carries a command, TextEvent carries a string being edited. • Components fire these events. • E.g., Button fires an ActionEvent. • E.g., TextArea fires a TextEvent. • All components fire WindowEvents. • When fired, events are routed to special Listener objects.
AWTEvent ActionEvent ComponentEvent TextEvent InputEvent WindowEvent MouseEvent KeyEvent The AWTEvent Hierarchy
Listeners • A listener is an object to which AWTEvents can be routed by components. • Different types of listeners. • One for each sub-class of AWTEvent. • E.g., ActionListener, ComponentListener, WindowListener. • A listener class is abstract: • Operations only; no attributes or methods. • Declared as an interface in Java.
Example: An ActionEvent Listener import java.awt.*; import java.applet.*; import java.awt.event.*; class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(“Button pressed!!”); } } public class ActionExample extends Applet { public void init() { Button button = new Button(“Push me”); button.addActionListener(new ButtonListener()); button.addMouseListener(new ButtonMouseListener()); add(button); } }
Example: A MouseEvent Listener class ButtonMouseListener implements MouseListener { public void mouseEntered(MouseEvent e) { System.out.println(“Mouse entered button!”); }; public void mouseExited(MouseEvent e) { System.out.println(“Mouse exited button!”); }; public void mousePressed(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} }
Mouse Events • mouseClicked(MouseEvent evt); • Called when the mouse button is clicked • mousePressed(MouseEvent evt); • Called when the mouse button is depressed • mouseReleased(MouseEvent evt); • Called when the mouse button is released
More Mouse Events • mouseEntered(MouseEvent evt); • Called when the mouse enters the component • mouseExited(MouseEvent evt); • Called when the mouse leaves the component • mouseMoved(MouseEvent evt); • Called when the mouse moves with no mouse button depressed • mouseDragged(MouseEvent evt); • Called when the mouse moves with the mouse button depressed
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