15.13. Chapter Summary

 
[Page 485 ( continued )]

Chapter Summary

  • The root class of the event classes is java.util.EventObject . The subclasses of EventObject deal with special types of events, such as action events, window events, component events, mouse events, and key events. You can identify the source object of an event using the getSource() instance method in the EventObject class. If a component can fire an event, any subclass of the component can fire the same type of event.

  • The listener object's class must implement the corresponding event-listener interface. Java provides a listener interface for every event class. The listener interface is usually named XListener for XEvent , with the exception of MouseMotionListener . For example, the corresponding listener interface for ActionEvent is ActionListener ; each listener for ActionEvent should implement the ActionListener interface. The listener interface contains the method(s), known as the handler(s) , which process the events.

  • The listener object must be registered by the source object. Registration methods are dependent on the event type. For ActionEvent , the method is addActionListener . In general, the method is named addXListener for X Event .

  • An inner class , or nested class, is a class defined within the scope of another class. An inner class can reference the data and methods defined in the outer class in which it nests , so you do not need to pass the reference of the outer class to the constructor of the inner class.

  • Convenience adapters are support classes that provide default implementations for all the methods in the listener interface. Java provides convenience listener adapters for every AWT listener interface with multiple handlers. A convenience listener adapter is named X Adapter for X Listener.


    [Page 486]
  • A source object may fire several types of events. For each event, the source object maintains a list of listeners and notifies all the registered listeners by invoking the handler on the listener object to process the event.

  • A mouse event is fired whenever a mouse is clicked, released, moved, or dragged on a component. The mouse-event object captures the event, such as the number of clicks associated with it or the location ( x - and y -coordinates) of the mouse point.

  • Java provides two listener interfaces, MouseListener and MouseMotionListener , to handle mouse events, implement the MouseListener interface to listen for such actions as mouse pressed, released, clicked, entered, or exited, and implement the MouseMotionListener interface to listen for such actions as mouse dragged or moved.

  • A KeyEvent object describes the nature of the event (namely, that a key has been pressed, released, or typed) and the value of the key.

  • The keyPressed handler is invoked when a key is pressed, the keyReleased handler is invoked when a key is released, and the keyTyped handler is invoked when a Unicode character is entered. If a key does not have a Unicode (e.g., function keys, modifier keys, action keys, and control keys), the keyTyped handler will be not be invoked.

  • You can use the Timer class to control Java animations. A timer fires an ActionEvent at a fixed rate. The listener updates the painting to simulate an animation.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net