1 / 17

Events (Chapter 11)

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().

josef
Download Presentation

Events (Chapter 11)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Events(Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth

  2. Events • Old Model (JDK 1.0.x) • Event Model Failed Miserably • New Model • Event classes • Event Listeners • Explicit event enabling • Adapters

  3. java.util.EventObject • Has no javadoc comments • Defines getSource()

  4. 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

  5. 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.

  6. 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.”

  7. Sample Code (p.313) class MyActionListener implments ActionListener { public void actionPerformed( ActionEvent ae ) { System.out.println( “Hi!” ); } }

  8. 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 ); } }

  9. Using Inner Classes public void init() { Button btn = new Button( “OK” ); btn.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { System.out.println( “Hi!” ); } } ); }

  10. 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 );

  11. 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.”

  12. 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 ); } }

  13. Listeners vs. Enabling • A subclass can handle its own events by adding itself as a listener. • In Explicit Event Handling, you control the order.

  14. 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.

  15. 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 }

  16. 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.

More Related