

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Spring 2005 Lab 10 - Advanced Event Driven Programming Handout
1. Lab Objectives Following the lab you should be able to: Write a program with a single listener that handles multiple event types from a single event source. Write a program with multiple listeners for a single event source. Write a program with one listener for multiple event sources. 2. Prior to the laboratory Review the laboratory handout. Review Wu, Chapter 7 and Chapter14. Read the tutorial, “Event Handling http://java.sun.com/docs/books/tutorial/uiswing/overview/event.html 3. Topics Topic Event-driven programming (review) Event handling including event receivers, listeners, event sources, event types, etc. (review) 4. Activities/Exercises 1. Modify a program to handle multiple event types from a single event source. 2. Modify a program to use multiple listeners for a single event source. 3. Write a program with one listener for multiple event sources. Activity 1: Modify a program to handle multiple event types from a single event source Download TempConverter.java from the class website. Note: this is the original version of TempConverter.java used in the first activity in the GUI lab – do not use the modified version of TempConverter.java you created in the GUI lab. Modify TempConverter.java to increase the text field size to 10. Modify TempConverter.java to add a MouseListener to the text entry field. The listener should implement the following behaviors: o When the mouse enters the text field, display the string ‘Enter value here” if the text field was blank. If the text field contains a value previously entered by the user when the mouse enters the text field, do not update the text field (i.e., do not overwrite a value entered by the user). o When the user clicks the mouse in the text field, clear the text field (i.e., set it to “”) o Using a try-catch block, add error handling to your TempConverter program. If a user enters a non-numeric value in the text field, clear the text entry field and set the Fahrenheit label to the string “Invalid value entered!”
Spring 2005 When you have your program working, demonstrate it for the lab instructor. Activity 2: Modify a program to use multiple listeners for a single event source Modify the TempConverter.java program from Activity 1 to add a FocusListener to the “Convert” button. The listener should implement the following behaviors (in addition to the behaviors already in place): o When the button gets focus, set the text (foreground) color on the button to Blue and print the following message to System.out, “Got focus”. o When the button loses focus, set the text (foreground) color on the button to Red and print the following message to System.out, “Lost focus”. Hint: to set the text color on the button, call the setForeground method with a parameter of Color.red or Color.blue When you have your program working, demonstrate it for the lab instructor.