Help is available for each task.
Task 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.
Create a class that implements the ItemListener
interface and define its itemStateChanged()
method.
To get the string to look up, use the getItem()
method of the ItemEvent
parameter to the method.
Use the Hashtable
.get()
method to look
for the item in the phonebook
.
Use the TextField
.setText()
method to
change the entryField
contents.
The Toolkit
.beep()
method plays a beep.
Use the Component
.getToolkit()
method to get a Component
to use.
Task 2
Register the ItemListener
with the Choice
object.
Call the Choice
.addItemListener()
method
with the instance of the class that implements the ItemListener
interface as the argument.
Task 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.
Create a class that implements the ActionListener
interface and define its actionPerformed()
method.
To get the string to look up, use the getText()
method of TextField
.
Use the Hashtable
.get()
method to look
for the item in the reversePhonebook
.
Use the Choice
.select()
method to move to
the appropriate item in the Choice
instance.
Use the Toolkit
.beep()
method to play a
beep if no match is found.
Task 4
Instantiate the class (once) that implements ActionListener
and register it with the TextField
and Button
instances.
Call the addActionListener()
method with the instance that
implements the interface as the argument.
Make sure the same listener is added to both classes. You do not need to
create two instances of the class that implements the interface.
Task 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 '-'.
Because only one method in the KeyListener
interface is needed, namely, keyTyped()
, it's more convenient to
subclass KeyAdapter
, which supplies the other methods in the
interface.
Use the Character
.isDigit()
method to
check for numerical input.
To reject inappropriate input, call the consume()
method of
the event.
Task 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.
The KeyEvent
class has constants for the four
characters to check.
Task 7
Register the object that implements KeyListener
with the TextField
instance.
Call the addKeyListener()
method with the class that implements
the interface as the argument.
Task 8
Use the Phone.html
file in the solution to test it.
Shift-Click on Phone.html to save.