// Form to update phone entry
import  java.awt.*;
import  java.awt.event.*;
import  javax.swing.*;
import  javax.swing.event.*;


public class UpdatePhone extends JFrame
   implements ActionListener , ItemListener
{
 GridBagConstraints gbc;
 GridBagLayout gbl;
 JTextField tf1,tf2;
 JButton b1,b2;
 PhoneBook pb;
 JComboBox jcbPhones;

   public UpdatePhone(PhoneBook pb)
   {
     super("Update Phone Entry");
     this.pb = pb;
     JLabel l1 = new JLabel("Name");
     jcbPhones = new JComboBox(pb.getNames());
     jcbPhones.addItemListener(this);
     JLabel l2 = new JLabel("Phone No. ");
     tf1 = new JTextField(20);  
     JLabel l3 = new JLabel("Type");
     tf2 = new JTextField(5);

     b1= new JButton("Update");
     b1.addActionListener(this);
     b2= new JButton("Exit");
     b2.addActionListener(this);
     gbl = new GridBagLayout();
     gbc  = new GridBagConstraints();
     gbc.anchor = GridBagConstraints.WEST;
     getContentPane().setLayout(gbl);
     displayComponent(l1,0,0,1,1);
     displayComponent(jcbPhones,1,0,2,1);
     displayComponent(l2,0,1,1,1);
     displayComponent(tf1,1,1,2,1);
     displayComponent(l3,0,2,1,1);
     displayComponent(tf2,1,2,1,1);
     displayComponent(b1,1,3,1,1);     
     displayComponent(b2,2,3,1,1);     

     setSize(300,200);
     setVisible(true);

   } // end of init

   public void itemStateChanged(ItemEvent evt)
   {
       PhoneEntry pe = pb.getPhoneEntry((String)evt.getItem());
       if ( pe == null)
             return;
       tf1.setText(pe.phoneno);
       tf2.setText(String.valueOf(pe.type));
    
   }
   public void actionPerformed(ActionEvent evt)
   {
     if ( evt.getSource() == b2 )
           System.exit(0);
     else
     {
      String name,phoneno;
      char ptype;

      name = tf1.getText();
      phoneno = tf2.getText();

    } // end of else

  }

 public void displayComponent(Component c, int x, int y, int w, int h)
 { 
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;
    gbl.setConstraints(c,gbc);
    getContentPane().add(c);
 } // end of displayComponent
 
 
} // end of class