Events - Octagon Software

Download Report

Transcript Events - Octagon Software

Events
(Chapter 11)
Java Certification Study Group
January 25, 1999
Mark Roth
Events
• Old Model (JDK 1.0.x)
– Event Model Failed Miserably
• New Model
–
–
–
–
Event classes
Event Listeners
Explicit event enabling
Adapters
java.util.EventObject
• Has no javadoc comments
• Defines getSource()
java.awt.AWTEvent
• Superclass of all AWT event classes.
• Event ID
– int getID()
– RESERVED_ID_MAX
• Two Ways to Handle:
– Delegate Event Handling to a Listener Object
– Explicitly Enable the originating Component to
Handle its own Events
Subclasses of AWTEvent
•
•
•
•
•
•
•
•
•
•
ActionEvent - Component Activated
AdjustmentEvent - Scrollbars
ContainerEvent - Components Added or Removed
FocusEvent - Component Receives Input Focus
ItemEvent - Item Selected from a List, Choice, Checkbox
KeyEvent - Keys Pressed or Released
MouseEvent - Mouse moved or buttons clicked
PaintEvent - Component is Painted
TextEvent - Text Component is Modified
WindowEvent - Iconify, Close, Maximize, etc.
Event Listeners
• “...an object to which a component has
delegated the task of handling a particular
kind of event...
1. Create a listener class that implements the
xxxListener interface
2. Construct the component
3. Construct an instance of the listener class
4. Call addxxxListener() on the component,
passing in the listener object.”
Sample Code (p.313)
class MyActionListener implments
ActionListener {
public void actionPerformed(
ActionEvent ae )
{
System.out.println( “Hi!” );
}
}
Sample Code Continued
public class ListenerTest extends Applet
public void init() {
Button btn = new Button( “OK” );
MyActionListener listener = new
MyActionListener();
btn.addActionListener( listener );
add( btn );
}
}
Using Inner Classes
public void init() {
Button btn = new Button( “OK” );
btn.addActionListener(
new ActionListener() {
public void actionPerformed(
ActionEvent ae )
{
System.out.println( “Hi!” );
}
}
);
}
Event Listeners Continued
• Eleven Listener Types (see Table 11.1)
• Multiple Listeners
– You Can Attach as Many as You Want
– No Guarantee as to Calling Order
– No Guarantee as to Same Thread
• Removing
– btn.removeActionListener( al );
Explicit Event Enabling
• To Hande: “
1. Create a subclass of the component
2. In the subclass constructor, call
enableEvents( AWTEvent.XXX_EVENT_MASK )
(See table 11.2 for possible event masks)
3. Provide the subclass with a
processXXXEvent() method; this method
should call the superclass’ version before
returning.”
Sample Code (p.315)
class MyBtn extends Button {
public MyBtn( String label ) {
super( label );
enableEvents( AWTEvent.ACTION_EVENT_MASK );
}
public void processActionEvent( ActionEvent ae ) {
System.out.println( “Hi!” );
super.processActionEvent( ae );
}
}
Listeners vs. Enabling
• A subclass can handle its own events by
adding itself as a listener.
• In Explicit Event Handling, you control the
order.
Adapters
• Event listeners often define many methods.
• Since they are interface, you must
implement all of them.
• Alternative: Subclass an appropriate adapter
(see table 11.3) and override only the one
you need.
Sample Code (p.318)
class MyIkeListener extends WindowAdapter {
public void windowIconified( WindowEvent we ) {
// Process the event.
}
// No need to declare:
//
windowActivated
//
windowClosed
//
windowClsing
//
windowDeactivated
//
windowDeiconified
//
windowOpened
}
References
• All Material in this Presentation is heavily
based on:
Roberts, Simon and Heller, Philip, Java 1.1
Certification Study Guide, 1997:
SYBEX™. ISBN: 0-7821-2069-5
• Selected portions from JDK 1.1.6 JavaDoc
HTML pages.