Download presentation
Presentation is loading. Please wait.
1
Lecture 9.5 Event Delegation
2
Event Handling How to Delegate events
Java event handling is based on interfaces, known as listeners, and a concept known a delegation. How to Delegate events (1) Create some class that ... • implements the listener for the appropriate kind of event • overrides the proper event handler method (2) Your code must ... (a) instantiate some object capable of generating events (eventGenObject). (b) instantiate an object (delegateObject) belonging to the class from (1). (c) call eventGenObject.addEventType Listener(delegateObject); © 2006 Pearson Addison-Wesley. All rights reserved
3
Example home of many event classes and listeners
import java.awt.event.*; public class BtnHandler implements ActionListener { public BtnHandler() {} public void actionPerformed(ActionEvent e) { System.out.println( “Another useless event message” ); } import javax.swing.JButton; ... JButton myButton; myButton = new JButton( “Button Label” ); myButton.setBounds(10, 10, 100, 20); win.add( myButton, 0 ); BtnHandler buttonHandler = new BtnHandler(); myButton.addActionListener( buttonHandler ); © 2006 Pearson Addison-Wesley. All rights reserved
4
EventTimer public abstract class EventTimer extends Timer
import java.awt.event.*; import javax.swing.Timer; public abstract class EventTimer extends Timer implements ActionListener { public EventTimer( int m ) { super(m, null); addActionListener( this ); } public abstract void actionPerformed(ActionEvent e); The “null” in the super call is another location where the delegate can be included. In other words, for this particular class it would work to use super(m,this) and not call addActionListener. © 2006 Pearson Addison-Wesley. All rights reserved
5
Keystroke Events Note that keystroke events are “generated” by window objects, such as JFrames. «interface» java.awt.event.KeyListener «event handler» + void keyPressed(java.awt.Event.KeyEvent e) + void keyReleased(java.awt.Event.KeyEvent e) + void keyTyped(java.awt.Event.KeyEvent e) java.awt.event.KeyEvent «query» + char getKeyChar() + int getKeyCode() getKeyChare returns the character associated with printable characters. getKeyCode returns a unique integer for EVERY keystroke. © 2006 Pearson Addison-Wesley. All rights reserved
6
JFrameWithKeys public class JFrameWithKeys extends JFrame
import java.awt.event.*; import javax.swing.JFrame; public class JFrameWithKeys extends JFrame implements KeyListener { public JFrameWithKeys(String s) { super(s); addKeyListener( this ); } public void keyPressed(KeyEvent e) { System.out.println( e.getKeyCode() ); public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } The “null” in the super call is another location where the delegate can be included. In other words, for this particular class it would work to use super(m,this) and not call addActionListener. © 2006 Pearson Addison-Wesley. All rights reserved
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.