Chapter 12 Graphical User Interface Components: Part 1 (Web hosting providers)
Chapter 12 Graphical User Interface Components: Part 1 661 textField1 handler This is the JTextField object. It contains an instance variable of type EventListenerList called listenerListthat it inherited from class JComponent. listenerList … This reference is created by the statement This is the TextFieldHandler object that implements ActionListenerand defines method actionPerformed. public void actionPerformed( ActionEvent event ) { // event handled here } textField1.addActionListener( handler ); Fig. 12.8Event registration for JTextFieldtextField1. 12. textField1.addActionListener( handler ); executes in Fig. 12.7, a new entry is placed in the listenerList for JTextField textField1, indicating both the reference to the listener object and the type of listener (in this case ActionListener). The type is important in answering the second question how does the GUI component know to call actionPerformedrather than another event handling method? Every JComponentactually supports several different event types, including mouse events, key events and others. When an event occurs, the event is dispatched only to the event listeners of the appropriate type. The dispatching of an event is simply calling the event handling method for each registered listener for that event type. Each event type has a corresponding event-listener interface. For example, Action- Events are handled by ActionListeners, MouseEvents are handled by MouseListeners (and MouseMotionListeners as we will see) and KeyEvents are handled by KeyListeners. When an event is generated by a user interaction with a component, the component is handed a unique event ID specifying the event type that occurred. The GUI component uses the event ID to decide the type of listener to which the event should be dispatched and the method to call. In the case of an ActionEvent, the event is dispatched to every registered ActionListener s actionPerformed method (the only method in interface ActionListener). In the case of a MouseEvent, the event is dispatched to every registered MouseListener (or MouseMotionListener, depending on the event that occurs). The event ID of the MouseEvent determines which of the seven different mouse event handling methods are called. All of this decision logic is handled for you by the GUI components. We discuss other event types and event-listener interfaces as they are needed with each new component we cover. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01