Web design conference - 660 Graphical User Interface Components: Part 1 Chapter

660 Graphical User Interface Components: Part 1 Chapter 12 Listener) can be supplied as an argument to this method. The object to which handler refers is an ActionListener because its class implements interface Action- Listener. Now, when the user presses Enter in any of these four fields, method action- Performed(line 65 90) in class TextFieldHandleris called to handle the event. Software Engineering Observation 12.2 The event listener for an event must implement the appropriate event-listener interface. Method actionPerformed uses its ActionEvent argument s method get- Source to determine the GUI component with which the user interacted and creates a String to display in a message dialog box. ActionEvent method getActionCommand returns the text in the JTextField that generated the event. If the user interacted with the JPasswordField, lines 83 84 cast the Component reference returned by event.getSource() to a JPasswordField reference so that lines 85 86 can use JPasswordField method getPassword to obtain the password and create the String to display. Method getPassword returns the password as an array of type char that is used as an argument to a String constructor to create a String. Line 89 displays a message box indicating the GUI component reference name and the text the user typed in the field. Note that even an uneditable JTextField can generate an event. Simply click the text field, then press Enter. Also note that the actual text of the password is displayed when you press Enter in the JPasswordField (of course, you would normally not do this!). Common Programming Error 12.4 Forgetting to register an event handler object for a particular GUI component s event type results in no events being handled for that component for that event type. Using a separate class to define an event listener is a common programming practice for separating the GUI interface from the implementation of its event handler. For the remainder of this chapter and Chapter 13, many programs use separate event-listener classes to process GUI events. 12.5.1 How Event Handling Works Let us illustrate how the event-handling mechanism works using textField1 from the preceding example. We have two remaining open questions from Section 12.4: 1. How did the event handler get registered? 2. How does the GUI component know to call actionPerformedas opposed to some other event handling method? The first question is answered by the event registration performed in lines 43 46 of the program. Figure 12.8 diagrams JTextField reference textField1, the JTextField object to which it refers and the listener object that is registered to handle the JText- Field s event. Every JComponent has an object of class EventListenerList (package javax.swing.event) called listenerList as an instance variable. All registered listeners are stored in the listenerList (diagramed as an array in Figure 12.8). When the statement Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply