Computer Science 122
Assignment 4
Due: October 5, 2002

Modify the program for assignment 3 by adding a button to the frame.  When it is clicked, the data should be displayed in the TextArea.  Also, divide your program into three classes, one for the data, the second for the list, and the third for the frame.  In the frame class, you should instantiate the list class and read the data into the list.  In the class for the button, you should call a method in the list class that will display the data in the text area.

When you are done with this program, add a second table to your database.  This table should be used to store data about borrowers.  It should have the borrowers’ IDs as well as names, addresses, etc.  You should decide what fields are needed.  Then create a second program to display the information about borrowers.

The following program should help with the button.

import java.io.*;
import java.awt.event.*;
import java.awt.*;

/* This example creates a frame with a button.  When the button is clicked,
data is displayed in the text area.  */
public class ShowFrame extends Frame
{
     private Panel panel;
     private Label areaLabel;
     private TextArea area;
     Button display;

     public ShowFrame ()
     {
          super ("Frame");
          setSize (300, 300);
 
          // Add a window closer to the frame.
          addWindowListener (new WindowAdapter()
               {public void windowClosing(WindowEvent e) {System.exit(0);}});
 
          // Get a new panel and set its background color.
          panel = new Panel ();
          panel.setBackground (Color.cyan);
 
          // Get a text area.
          areaLabel = new Label ("Text Area");
          area = new TextArea (10, 30);
 
          // Get a new button and add an action listener to it.
          display = new Button ("Display Data");
          display.addActionListener (new DisplayListener ());
 
          // Add everything to the panel.
          panel.add (areaLabel); panel.add (area); panel.add (display);
          add (panel);
     } // constructor

     // An inner class that displays the data when the button is clicked.
     class DisplayListener implements ActionListener
     {
          public void actionPerformed (ActionEvent e)
          {
               Data data = new Data ();
               data.displayData (area);
          } // actionPerformed
     } // DisplayListener

     // The main method gets a new frame and shows it.
     public static void main (String [] args)
     {
          ShowFrame frame = new ShowFrame ();
          frame.setVisible (true);
     }
} // class ShowFrame

// The Data class is used to store name, email and phone data.
class Data
{
     private String name, email, phone;
 
     // The constructor assigns values to the variables.
     Data ()
     {
          name = "Alice Lee";
          email = "alee@aol.com";
          phone = "123-45-6789";
     } // constructor
 
     // displayData displays the data on the screen.
     protected void displayData (TextArea area)
     {
          area.append ("Name: " + name + '\n');
          area.append ("Email: " + email + '\n');
          area.append ("Telephone: " + phone + '\n');
     } // displayData
} // class Data