UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out the required task. This is Event Handling Execution
UQC117S2 Graphics Programming Lecture 2 How Can This Happen? Bottom level: Software cycles round in a loop, waiting for an event. Event loop JAVA does this for us. All we have to do is implement the methods that JAVA invokes when an event occurs –In invoking these methods, JAVA also provides useful info. about the event
UQC117S2 Graphics Programming Lecture 2 How Do Events Get Captured? 3 things to do –Declare that the class implements the relevant listener interface –Implement the necessary listener methods –Register that a component is going to listen out for an event
UQC117S2 Graphics Programming Lecture 2 Interfaces –“Defines a protocol of behaviour” –Allows JAVA to provide a sort of multiple inheritance, without the attendant problems – The behaviour enforced by the interface is that these methods must be implemented.
UQC117S2 Graphics Programming Lecture 2 Example The MouseListener interface defines the following 5 abstract methods: –mouseClicked(MouseEvent) –mouseEntered(MouseEvent) –mouseExited(MouseEvent) –mousePressed(MouseEvent) –mouseReleased(MouseEvent) A class that declares that it will implement the MouseListener interface is “signing a contract” to say that it will implement all 5 methods.
UQC117S2 Graphics Programming Lecture 2 Syntax public class MyClass implements MouseListener{ A class can implement several listeners. Listeners are separated by a comma. Example: public class MyClass implements WindowListener, MouseListener{ Which methods belong to each Listener? How will you know?
UQC117S2 Graphics Programming Lecture 2 Register that a class is Listening for an Event Like registering on a mailing list for an interest group By registering as an event listener, we are requiring JAVA to use the event handling methods – the signatures of which are defined in the event interface- implemented in this class to handle given events Many of the addXXXXListener methods are defined in Component, others are defined further down e.g. addWindowListener is a method belonging to the Window class Registering a class as a listener amounts to including the line: AComponent.addxxxListener(this); In your class
UQC117S2 Graphics Programming Lecture 2 Example To register that a class listens out for action events …………… Jbutton button1; …………… button1=new Jbutton(“First button”); add(button1); button1.addActionListener(this ); ………………… Why button1.addActionListener? What else must be in this class definition?
UQC117S2 Graphics Programming Lecture 2 Example Responding to an action event,generated by pressing a Jbutton Only one method to be implemented – actionPerformed The ActionEvent object in the argument has a method called getActionCommand() – returns a String public void actionPerformed(ActionEvent event) { String command; String button1 = “Button 1”; String button2 = “Button 2”; command =event.getActionCommand(); if(command.equals(button1){ System.out.println(“Button 1”); }else if (command.equals(button2){ System.out.prinltn(“Button 2”); } Check the API documentation to see what methods aresupplied with MouseEvent, ActionEvent, FocusEvent, AdjustmentEvent, ComponentEvent, ItemEvent, KeyEvent, TextEvent, and WindowEvent classes
UQC117S2 Graphics Programming Lecture 2 Implement the Necessary Listener Methods This means all of them Often seems rather untidy We can avoid this untidiness – see later Note the argument to all AWT listener methods –What is this argument? –Instance of a class descended from java.awt.AWTEvent –These classes provide methods to return useful details about the event just captured –JAVA sets up the attributes of this object, just use the method to get the details
UQC117S2 Graphics Programming Lecture 2 Adapter Classes (1) A solution to the “untidy” interface implementation For listener interfaces with > 1 method, AWT supplies an adapter class Adapters implement all of an interface’s methods as “do nothing” methods
UQC117S2 Graphics Programming Lecture 2 Adapter Classes (2) A subclass of the adapter class need therefore only implement the methods of interest Example public class MyClass extends MouseAdapter{ public void mouseClicked(MouseEvent event){ ……… (Handling of mouse click goes in here) …………… }
UQC117S2 Graphics Programming Lecture 2 Multiple Inheritance PROBLEM! If MyClass inherits from, say, JFrame, it can’t also inherit from MouseAdapter. SOLUTION! Inner classes –An inner class is a “class within a class –An inner class can be named or anonymous –If an inner class is used, the implement phrase is no longer required
UQC117S2 Graphics Programming Lecture 2 Named Inner Class public class MyClass extends Jframe{ …………… aComponent.addMouseListener(new MyAdapter)); …………… class MyAdapter extends MouseAdapter{ public void mouseClicked(MouseEvent event) {……… Handling of mouse click goes in here ……… }
UQC117S2 Graphics Programming Lecture 2 Example – Anonymous Inner Class NOT RECOMMENDED public class MyClass extends Jframe{ …………………………………… aComponent.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent event){ …………… Handling of mouse click in here ………… } }); …………… }