Event Information o An event object contains whatever properties are pertinent to the event You can identify the source object of the event using the getsourcen instance method in the eventobiect class o The subclasses of Eventobject deal with special types of events such as button actions window events component events, mouse movements, and keystrokes. Table 12.1 lists external user actions, source objects, and event types generated Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Event Information ⚫An event object contains whatever properties are pertinent to the event. ⚫You can identify the source object of the event using the getSource() instance method in the EventObject class. ⚫The subclasses of EventObject deal with special types of events, such as button actions, window events, component events, mouse movements, and keystrokes. Table 12.1 lists external user actions, source objects, and event types generated
Selected user actions Event Type User action Object Generated Click a button JBut七。n Ac七i。 eVent Click a check box CHeckbOx 工 temEvent, Actioneven七 Click a radio button Radi。 Button ItemEvent ActionEvent Press return on a text field JTextField Ac七i。 nEven Select a new item JComboBox 工七 emEter七, Actionevent Window opened, closed, etc. W主ndow W主 ndowEvent Mouse pressed, released, etc Component Mouseevent Key released, pressed, etc Component KeyEvent Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Selected User Actions Source Event Type User Action Object Generated Click a button JButton ActionEvent Click a check box JCheckBox ItemEvent, ActionEvent Click a radio button JRadioButton ItemEvent, ActionEvent Press return on a text field JTextField ActionEvent Select a new item JComboBox ItemEvent, ActionEvent Window opened, closed, etc. Window WindowEvent Mouse pressed, released, etc. Component MouseEvent Key released, pressed, etc. Component KeyEvent
Event-Handling model ● Event-handling model O Three parts ● Event source GUi component with which user interacts ● Event object Encapsulates information about event that occurred ● Event listener Receives event object when notified, then responds O Programmer must perform two tasks o Register event listener for event source Implement event-handling method (event handler Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Event-Handling Model ⚫ Event-handling model Three parts ⚫Event source • GUI component with which user interacts ⚫Event object • Encapsulates information about event that occurred ⚫Event listener • Receives event object when notified, then responds Programmer must perform two tasks ⚫Register event listener for event source ⚫Implement event-handling method (event handler)
The Delegation model Register by invoking Trigger an event source. addXlistener(listener); User source: sourceclas listener: Listener Class Action +addXListener(LIstener listener) Keep it a list LIstener event: XEvent listener 1 Invoke listener 2 listener l handler(event) handler(XEvent event istener2. handler(event) histenern stenen handler(event) Internal function of the source object Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 The Delegation Model source: SourceClass +addXListener(XListener listener) User listener: ListenerClass Action Trigger an event XListener +handler(XEvent event) Internal function of the source object event: XEvent listener1 listener2 … listenern +handler( XEvent Register by invoking source.addXListener(listener); Keep it a list Invoke listener1.handler(event) listener2.handler(event) … listenern.handler(event)
How Event Handling works o Two open questions from OHow did event handler get registered? ● Answer Through components method addAction Listener OHow does component know to call action Performed? ● Answer: Event is dispatched only to listeners of appropriate type Each event type has corresponding event-listener interface Event ID specifies event type that occurred For example: Mouse Event event's ID spedify which method of the seven events will be handler Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 How Event Handling Works ⚫Two open questions from How did event handler get registered? ⚫Answer: • Through component’s method addActionListener How does component know to call actionPerformed? ⚫Answer: • Event is dispatched only to listeners of appropriate type • Each event type has corresponding event-listener interface Event ID specifies event type that occurred For example: MouseEvent event’s ID spedify which method of the seven events will be handler