656 Graphical User Interface Components: Part 1 Chapter (Web host forum)
656 Graphical User Interface Components: Part 1 Chapter 12 An event listener object listens for specific types of events generated by event sources (normally GUI components) in a program. An event handler is a method that is called in response to a particular type of event. Each event-listener interface specifies one or more event-handling methods that must be defined in the class that implements the event-listener interface. Remember that interfaces define abstractmethods. Any class that implements an interface must define all the methods of that interface; otherwise, the class is an abstractclass and cannot be used to create objects. The use of event listeners in event handling is known as the delegation event model the processing of an event is delegated to a particular object (the listener) in the program. When an event occurs, the GUI component with which the user interacted notifies its registered listeners by calling each listener s appropriate event handling method. For example, when the user presses the Enter key in a JTextField, the registered listener s actionPerformed method is called. How did the event handler get registered? How does the GUI component know to call actionPerformed as opposed to some other event handling method? We answer these questions and diagram the interaction as part of the next example. 12.5 JTextFieldand JPasswordField JTextFields and JPasswordFields (package javax.swing) are single-line areas in which text can be entered by the user from the keyboard or text can simply be displayed. A JPasswordFieldshows that a character was typed as the user enters characters, but hides the characters, assuming that they represent a password that should remain known only to the user. When the user types data into a JTextField or JPasswordField and presses the Enter key, an action event occurs. If the program registers an event listener, the listener processes the event and can use the data in the JTextFieldor JPassword- Fieldat the time of the event in the program. Class JTextFieldextends class JTextComponent(package javax.swing.text), which provides many features common to Swing s text-based components. Class JPasswordFieldextends JTextFieldand adds several methods that are specific to processing passwords. Common Programming Error 12.3 Using a lowercase fin the class names JTextFieldor JPasswordFieldis a syntax error. The application of Fig. 12.7 uses classes JTextField and JPasswordField to create and manipulate four fields. When the user presses Enter in the currently active field (the currently active component has the focus ), a message dialog box containing the text in the field is displayed. When an event occurs in the JPasswordField, the password is revealed. 1 // Fig. 12.7: TextFieldTest.java 2 // Demonstrating the JTextField class. 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; Fig. 12.7 Demonstrating JTextFields and JPasswordFields (part 1 of 4). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01