Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

Phonebook Searching

 

Training Index

by jGuru

Help  API Docs  About This Short Course  Short Course  Exercises

With this exercise, you will complete a phonebook look-up applet. The skeleton code creates a GUI that consists of a pull-down list of names, a text field for entry of a phone number to look up, and a command button to activate the look-up operation. It is your job to add the event handling to the applet.

There are three different events that must be handled. When the user changes the name in the pull-down list, the applet should look up the appropriate telephone number in the phonebook. When the user enters a phone number in the text field and presses return or selects the button, the applet should look up the number in the reverse phonebook and change the name in the pull-down list. Finally, the applet should restrict input in the text field to digits and the '-' character. If a look-up operation fails, the applet should simply beep.

The actual telephone book appears below:

Name Number
Mom 555-1111
Dad 555-2222
Hon 555-3333
Boss 555-4444
Broker 555-5555
Jenny 867-5309

Prerequisites

Skeleton Code

Tasks

1. To handle selections in the Choice object, design a class that implements ItemListener. When an item is selected, the applet should look up the name in the phonebook hashtable and display the number found in the entryField text field.
2. Register the ItemListener with the Choice object.
3. To handle text entry and the look-up operation for the telephone number, design a class that implements ActionListener. The phone number from the text field should be looked up in the reversePhonebook hashtable and the appropriate name displayed in the bookChoice pull-down list.
4. Instantiate the class (once) that implements ActionListener and register it with the TextField and Button instances.
5. To restrict input in the TextField, design a class that implements KeyListener or subclasses KeyAdapter. Use this class to reject textfield input that is not a digit or '-'.
6. To ensure errors can be deleted and retain normal functionality for the Enter and Tab keys, let the Backspace, Delete, Tab, and Enter keys be valid characters for the keyTyped() method also.
7. Register the object that implements KeyListener with the TextField instance.
8. Use the Phone.html file in the solution to test it.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

This exercise only runs within browsers that support the Java Runtime Environment 1.1 and appletviewer. If you are using an appropriate browser, try this exercise. The output should look similar to the following:

PHONE.GIF

Next Exercise

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.