90 likes | 192 Views
Programming Languages. The Java Event Model. In this lesson, we look at the content of these objects and how they are processed in Java 2. Events In Java 2.
E N D
The Java Event Model In this lesson, we look at the content of these objects and how they are processed in Java 2.
Events In Java 2 • Java 2 handles events that are generated in a graphical component by delegating to objects that have registered an interest in that type of event. • The observable component that generates events, and the observer is the object that has registered to receive events. • Event observer objects are called listeners and implement listener interfaces.
The Event Hierarchy • The root of the Java 2 event hierarchy is the EventObject class • java.uti1.EventObject +--java.awt.AWTEvent +-- java.awt.event.ActionEvent +-- java.awt.event.AdjustmentEvent +-- java.awt.event.AncestorEvent +-- java.awt.event.ComponentEvent +-- java.awt.event.InputMethodEvent +-- java.awt.event.InternalFrameEvent +-- java.awt.event.InvocationEvent +-- java.awt.event.ItemEvent +-- java.awt.event.TextEvent
Events that descend from ComponentEvent • For typical programming, user probably works with only a few of the events: ComponentEvent • java.awt.event.ComponentEvent +-- java.awt.event.ContainerEvent +-- java.awt.event.FocusEvent +-- java.awt.event.PaintEvent +-- java.awt.event.WindowEvent +-- java. awt.event.InputEvent +-- java.awt.event.MouseEvent +-- java.awt.event.KeyEvent
Listeners And Adapters • Java defines a number of listener interfaces that are named according to the events they process. • Java also includes classes called adapters that provide empty methods for the more complex interfaces. • Each class that can generate one of these events has a method to add a listener for that event.
An Java Example with Event-Handling • This Java Applet has a button, with caption “Click Me”. When user clicks the button, it beeps.