We are going to take the phone look-up assignment you just completed and replace the text-based interface with a graphical user interface. In addition, we are going to add some extra features, such as the ability to create new phone records.
Your application will have a main window that allows users to look up record information for phone numbers they enter. The main window will have a text field where the user can enter a number and a search button to the right of that field. Below the field and the search button will be a text area where the application can display name and address information that results from searches.
![]() |
A text area is implemented via the class JTextArea. The most important difference between a text area and a text field is that a text area can accomodate multiple lines of text. Here is some sample code that demonstrates how to put multiple lines of text into a text area. The sample assumes that we have already created a JTextArea member variable named 'textAreaOne'.
String name = "Joe Gregg"; String address = "413 Briggs Hall"; textAreaOne.setText(name + "\n" + address);
The main window will also have a menu bar at the top. The menu bar will have a File menu with a Save command to save all the records and an Exit command to exit from the application. The Edit menu will have a command to add a new entry to the record set.
The Edit/New Entry... menu command will bring up the dialog for creating new PhoneData records.
![]() |
You can use the designer to add a JDialog object to your frame class and then set up the elements of the dialog. When the user selects Edit/New Entry... your application will make the dialog appear by calling the dialog object's setVisible(true) method.
The Cancel button will simply dismiss the dialog by calling the dialog object's setVisible(false) method. The OK button will read the text the user has typed into the three fields, construct a PhoneData object from that text, add that PhoneData object to the application's PhoneBook, and then dismiss the dialog by calling setVisible(false).
An important new feature we are going to implement in this version is the ability to create new entries for the phone book and then save our changes back to the data file.
The first and most obvious change we will need to make is to add a couple of methods to the PhoneBook interface that will allow us to put new entries in the phone book and then save the phone book:
public interface PhoneBook {
// Add a new entry to the phone book
public void addEntry(PhoneData entry);
// Return true if the phone book contains an entry
// with the given phone number
public boolean hasEntry(String number);
// Fetch the entry with the given number
public PhoneData getEntry(String number);
// Save all of our entries to a text file
// with the given name.
public void saveToFile(String fileName);
}
The File/Save command will save all of the PhoneData objects stored in your Map back to the phones.txt file. The first thing you will need is some way of getting those objects back out of your Map. You can do this by calling the Map's values() method, which returns a Collection of PhoneData objects. Assuming that the Map member variable we are using is named 'myMap', we would write something like this:
Collection<PhoneData> records = myMap.values();
You then need to write some code that will iterate over the collection of records to write them out to the data file. The following code shows how to set up that loop:
Iterator<PhoneData> iter = records.iterator();
while(iter.hasNext()) {
PhoneData record = iter.next();
// Put code here to write the record out to the file
}
To write data to the text file, you should use the java.io.PrintWriter class. The following code shows how to use a PrintWriter to write some lines of text to a file.
PrintWriter out = null;
try {
out = new PrintWriter("myFile.txt");
} catch(Exception ex) {
System.out.println("Unable to open output file.");
System.out.println(ex.getMessage());
System.exit(1);
}
out.println("Hello!");
out.println("This is a test.");
out.flush();
out.close();