Abteilung für Telekooperation Übung Softwareentwicklung 2 für Wirtschaftsinformatik Event Listeners Dr. Wieland Schwinger, MSc. Abteilung für Telekooperation Don't call us! we call you! -- the Hollywood principle Abteilung für Telekooperation Handling Events • Every time the user types a character or pushes a mouse button, an event occurs • Objects can be notified by the occurrence of the event. • For this the object has to implement the event listener interface. • Some examples of swing component generated events: – – – – – – – ActionListener, WindowListener, MouseListener, MouseMotionListener, ComponentListener, FocusListener, ListSelectionListener, … Abteilung für Telekooperation Java Event Mechanism • Event Source: – holds list of Event Listerner interested in the Event Source's signaling of the occurance of an Event and fires an Event on it's demand – to subscribe an Event Listeners • addXXXEventListener (XXXEventListener) – to descubsribe an Event Listeners • removeXXXEventListener (XXXEventListener) – to signal the occurence of an event to the subscribed Event Listeners • fireXXXEvent() • Event Object: – the signalled event is an object of type EventObject • class XXXEvent extends java.util.EventObject { ... } • Event Listener: – the Event Listeners are informed about an event's occrence by calling the dedicated method and passing the event object • interface XXXEventListener extends java.util.EventListener { XXXEventOccurred (XXXEvent e) { // react on the event } } Abteilung für Telekooperation Beispiel: Interfaces für Ereignisbehandlung • Implementieren Sie ein geeignetes Java-Ereignisszenario, das auf Eingaben von der Konsole mit einem Ereignis reagiert. Hinweise: Es sollen die folgenden Teile des Ereignismechanismus implementiert werden: – InputEvent (Ereignisobjekt mit Daten) – InputEventListener (Definition einer Schnittstelle für Ereignisempfänger) – InputTrigger (Ereignisquelle mit Registrierungsmöglichkeit für Empfänger und Mechanismus zum Auslösen des Ereignisses) Die Ereignisquelle (InputTrigger) liest die Eingabe in folgender run()-Methode: public void run () { try { do { int in = System.in.read(); fireInputEvent(this, (byte) in); } while (in != (int) 'q'); } catch (Exception e) { /* handle exception here */ } } Abteilung für Telekooperation Beispiel: Interfaces für Ereignisbehandlung Datei InputEventListener.java Datei InputEvent.java import java.util.*; public interface InputEventListener extends EventListener { public void inputReceived (InputEvent e); 4 } import java.util.*; public class InputEvent extends EventObject { 1 public char data; InputEvent (Object sender, char data) { 2 super(sender); this.data = data; } } Datei MyHandler.java public class MyHandler extends Thread implements InputEventListener { 1 MyHandler(InputTrigger t) { t.addInputEventListener(this); } 2 public void inputReceived (InputEvent e) { // here I do what ever I want // if a new input has been received, e.g. System.out.println("new input: " + e.data); } } 3 Abteilung für Telekooperation Beispiel: Interfaces für Ereignisbehandlung Datei InputTrigger.java 3 import java.util.*; public class InputTrigger implements Runnable { protected Vector listeners = new Vector(); public void addInputEventListener (InputEventListener l) { listeners.addElement(l); } public void removeInputEventListener(InputEventListener l) { listeners.removeElement(l); } private void fireInputEvent (Object sender, byte data) { System.out.println("InputEvent triggered"); Iteration iter = listeners.iterator(); while (iter.hasNext()) { InputEvent e = new InputEvent(sender, data); ((InputEventListener) iter.next()).inputReceived(e); } } 4 public void run () { try { char in; do { in = (char) System.in.read(); fireInputEvent(this, in); } while (in != (char) 'q'); } catch (Exception e) { /* some error handling */ } } public static void main (String[] args) { InputTrigger t = new InputTrigger(); t.run(); MyHandler theListener = new MyHandler(t); theListener.start(); } } 2 1 Abteilung für Telekooperation How to implement an Event Handler • To realize e.g. an ActionListerner: – Declaration of the event handler class public class MyClass implements ActionListener { ... – Register an instance of the event handler class as a listener upon one (more) event source (components, e.g. a Button) Button signallingComponent = new Button("Click Me"); signallingComponent.addActionListener(instanceOfMyClass); – Implementing methods of the listener interface, e.g.: public void actionPerformed(ActionEvent e) { ... // code that reacts to the action } • The signaling of the event (by generating the event object and invoking the actionPerformed method) is realized in the AWT/Swing package Abteilung für Telekooperation Event Object • An event is represented by an instance of EventObject. • EventObject is the superclass for all AWT and Swing events. • The method getSource() returns the object that fired the event. Abteilung für Telekooperation Mouse Listener Example /* Example 1: implementing the MouseListener Interface */ public class MyClass implements MouseListener { signaler of the event, e.g. component like a button ... someObject.addMouseListener(this); pass object as listener to ... subscribe to the signaler public void mousePressed(MouseEvent e) { ... } public void mouseReleased(MouseEvent e) { ... } public void mouseEntered(MouseEvent e) { ... } public void mouseExited(MouseEvent e) { ... } public void mouseClicked(MouseEvent e) { ...} } Abteilung für Telekooperation Mouse Listener Example /* Example 2: extending the MouseAdapter class */ public class MyClass extends MouseAdapter { class of AWT package ... implementing the someObject.addMouseListener(this); EventListerner interface ... public void mouseClicked(MouseEvent e) { ...//Event handler implementation goes here... …e.getX() …e.getY()) } } Abteilung für Telekooperation Mouse Listener Example /* Example 3: using an inner class */ public class MyClass extends Applet { ... someObject.addMouseListener(new MyAdapter()); ... realized as inner class class MyAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e) { ...//Event handler implementation goes here... } } } Abteilung für Telekooperation Events Handling and Threads • Event-handling code executes in a single thread, the event-dispatching thread. • "Painting code" also executes in the eventdispatching thread. • Consequently, program's GUI frozen while executing event handler – for long-running event handler additional thread recommended Abteilung für Telekooperation Further Readings • Java.sun.com tutorial • Important Classes and APIs: – – – – – EventObject Mouse Event Mouse Listener API Mouse-Motion Event Mouse-Motion Listener API