Greeting Applet: Handling Action Events r We will now add code to process the pressing of the ENTER key r Event sources generate or trigger events and Event listeners process the generated events r For this applet, the TextField object inputLine is the event source and GreetingApplet is the event listener. r TextField inputline generates an action event C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 6 GreetingApplet: Handling Action Events We will now add code to process the pressing of the ENTER key. Event sources generate or trigger events and Event listeners process the generated events. For this applet, the TextField object inputLine is the event source and GreetingApplet is the event listener. TextField inputLine generates an action event
GreetingApplet as an Action Event Handler r To set a Greeting Applet object be an action event handler, we must 1. Import the Java event-handling classes 2. Modify the class declaration to include the clause implements Action Listener. 3. Add the action Performed method to the class definition 4. Register the Greeting Applet object to the event source inputline as its action event listener. (An event source will call the registered listeners action Performed method when the event occurs.) C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 7 GreetingApplet as an Action Event Handler To set a GreetingApplet object be an action event handler, we must 1. Import the Java event-handling classes. 2. Modify the class declaration to include the clause implements ActionListener. 3. Add the actionPerformed method to the class definition. 4. Register the GreetingApplet object to the event source inputLine as its action event listener. (An event source will call the registered listeners’ actionPerformed method when the event occurs.)