/* Written by John Avitabile

   Session 5 Example 1

   Changing font size

   And we can change other properties in class

*/

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class ex1 extends JFrame implements ActionListener, WindowListener

{

 

 private JLabel label1 = new JLabel("Side 1");

 private JLabel label2 = new JLabel("Side 2");

 private JLabel label3 = new JLabel("Hypotenuse");

 

 private JTextField textField1 = new JTextField(5);

 private JTextField textField2 = new JTextField(5);

 private JTextField textField3 = new JTextField(10);

 

 private JButton button = new JButton("Find Hypotenuse");

 

 private Timer myTimer = new Timer(3000, this);

 

 public static void main(String args[])

 {

   ex1 myFrame = new ex1("Pythagoras");

   myFrame.addWindowListener(myFrame);

 }

 

 ex1(String s)

 {

  super(s);

  setBounds(100, 200, 500, 300);

  setup();

  Font large = new Font("Arial", Font.BOLD + Font.ITALIC, 18);

  label1.setFont(large);

  label2.setFont(large);

  label3.setFont(large);

  show();

  myTimer.setRepeats(false); // timer only goes once

  myTimer.addActionListener(this);

 }

 

 


 public void setup()

 {

  Container c = getContentPane();

  // set layout manager

  c.setLayout(new GridLayout(4,2,10,10));

  c.add(label1);

  c.add(textField1);

  c.add(label2);

  c.add(textField2);

  c.add(label3);

  c.add(textField3);

  c.add(button);

  button.addActionListener(this);   

 }

 

 public void actionPerformed(ActionEvent e)

 {

  if (e.getSource() == button)

  {

    myTimer.start();

    double side1 = Double.parseDouble(textField1.getText().trim());

    double side2 = Double.parseDouble(textField2.getText().trim());

    double side3 = Math.sqrt(Math.pow(side1,2) + Math.pow(side2,2));

    textField3.setText(Double.toString(side3));

  }

  else if (e.getSource() == myTimer)

  {

    textField1.setText("");

    textField2.setText("");

    textField3.setText("");

  }

 }

 

 public void windowActivated( WindowEvent e )

 {

 }

 

 public void windowClosed( WindowEvent e )

 {

 }

 

 public void windowClosing( WindowEvent e )

 {

    System.exit(0);

 }

 

 public void windowDeactivated( WindowEvent e )

 {

 }

 

 public void windowDeiconified( WindowEvent e )

 {

 }

 

 public void windowOpened( WindowEvent e )

 {

 }

 

 public void windowIconified( WindowEvent e )

 {

 }

}

 


/* Written by John Avitabile

   Session 5 Example 2

   Using the BetterTextField class

   In class, we'll add more methods to BetterTextField

*/

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class ex2 extends JFrame implements ActionListener, WindowListener

{

 

 Font large = new Font("Arial", Font.BOLD + Font.ITALIC, 18);

 

 private JLabel label1 = new JLabel("Side 1");

 private JLabel label2 = new JLabel("Side 2");

 private JLabel label3 = new JLabel("Hypotenuse");

 

 private BetterTextField textField1 = new BetterTextField(large);

 private BetterTextField textField2 = new BetterTextField(large);

 private BetterTextField textField3 = new BetterTextField(large);

 

 private JButton button = new JButton("Find Hypotenuse");

 

 private Timer myTimer = new Timer(3000, this);

 

 public static void main(String args[])

 {

   ex2 myFrame = new ex2("Pythagoras");

   myFrame.addWindowListener(myFrame);

 }

 

 ex2(String s)

 {

  super(s);

  setBounds(100, 200, 500, 300);

  setup();

  textField3.setColor(Color.red, Color.blue);

  show();

  myTimer.setRepeats(false); // timer only goes once

  myTimer.addActionListener(this);

 }

 

 


 public void setup()

 {

  Container c = getContentPane();

  c.setLayout(new GridLayout(4,2,10,10));

  c.add(label1);

  c.add(textField1);

  c.add(label2);

  c.add(textField2);

  c.add(label3);

  c.add(textField3);

  c.add(button);

  button.addActionListener(this);   

 }

 

 public void actionPerformed(ActionEvent e)

 {

  if (e.getSource() == button)

  {

    myTimer.start();

    textField3.colorSwitch();

    double side1 = Double.parseDouble(textField1.getText().trim());

    double side2 = Double.parseDouble(textField2.getText().trim());

    double side3 = Math.sqrt(Math.pow(side1,2) + Math.pow(side2,2));

    textField3.setText(Double.toString(side3));

  }

  else if (e.getSource() == myTimer)

  {

    textField1.setText("");

    textField2.setText("");

    textField3.setText("");

  }

 }

 

 public void windowActivated( WindowEvent e )

 {

 }

 

 public void windowClosed( WindowEvent e )

 {

 }

 

 public void windowClosing( WindowEvent e )

 {

    System.exit(0);

 }

 

 public void windowDeactivated( WindowEvent e )

 {

 }

 

 public void windowDeiconified( WindowEvent e )

 {

 }

 

 public void windowOpened( WindowEvent e )

 {

 }

 

 public void windowIconified( WindowEvent e )

 {

 }

}

 


 

import java.awt.*;

import javax.swing.JTextField;

 

public class BetterTextField extends JTextField

// can only extend classes that are not final

{

 

  public BetterTextField(Font f)

  {

    super(); // call JTextField's constructor

    this.setFont(f); // use JTextField's setFont method

  }

 

  public void setColor(Color fore, Color back)

  {

    this.setForeground(fore);

    this.setBackground(back);

  }

 

  public void colorSwitch()

  {

    Color temp1 = this.getForeground();

    Color temp2 = this.getBackground();  

    this.setForeground(temp2);

    this.setBackground(temp1);

  }

}

 


/* Written by John Avitabile

   Session 5 Example 3

   Using the Person class

*/

 

import java.util.ArrayList;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class ex3 extends JFrame implements ActionListener, WindowListener

{

 

 private JLabel nameLabel = new JLabel("Name");

 private JLabel phoneLabel = new JLabel("Phone");

 private JTextField nameField = new JTextField();

 private JTextField phoneField = new JTextField();

 private JButton first = new JButton("First");

 private JButton previous = new JButton("Previous");

 private JButton next = new JButton("Next");

 private JButton last = new JButton("Last");

 private JLabel position = new JLabel();

 private int index = 0;

 private static ArrayList data = new ArrayList();

 

 public static void main(String args[])

 {

   assignData();

   ex3 myFrame = new ex3("Phone Book");

   myFrame.addWindowListener(myFrame);

 }

 

 public static void assignData()

 {

   data.add(new Person("Mary", "222-2222"));

   data.add(new Person("Joe", "333-3333"));

   data.add(new Person("Alice", "444-4444"));

   data.add(new Person("Sam", "555-5555"));

 }

 

 ex3(String s)

 {

  super(s);

  setBounds(100, 100, 510, 400);

  setup();

  show();

 }

 

 


 public void setup()

 {

  Container c = getContentPane();

  c.setLayout(null);

  c.add(nameLabel);

  c.add(nameField);

  c.add(phoneLabel);

  c.add(phoneField); 

  c.add(first);

  c.add(previous);

  c.add(next);

  c.add(last);

  c.add(position);

  nameLabel.setBounds(10, 30, 150, 30);

  nameField.setBounds(170, 30, 150, 30);

  phoneLabel.setBounds(10, 80, 150, 30);

  phoneField.setBounds(170, 80, 150, 30);

  first.setBounds(10, 330, 90, 30);

  previous.setBounds(110, 330, 90, 30);

  next.setBounds(210, 330, 90, 30);

  last.setBounds(310, 330, 90, 30);

  position.setBounds(410, 330, 90, 30);

  first.addActionListener(this);   

  previous.addActionListener(this);   

  next.addActionListener(this);   

  last.addActionListener(this);

  position.setText("1/" + data.size());

  Person p = (Person) data.get(index);

  phoneField.setText(p.getPhone());

  nameField.setText(p.getName()); 

 }

 

 public void actionPerformed(ActionEvent e)

 {

  if (e.getSource() == first)

  {

    index = 0;

  }

  else if (e.getSource() == previous)

  {

    if (index > 0)

      index--;

  }

  else if (e.getSource() == next)

  {

    if (index + 1 < data.size())

      index++;

  }

  else if (e.getSource() == last)

  {

    index = data.size()-1;

  }

  Person p = (Person) data.get(index);

  phoneField.setText(p.getPhone());

  nameField.setText(p.getName()); 

  position.setText((index+1) + "/" + data.size());   

 }

 

 public void windowActivated( WindowEvent e )

 {

 }

 

 public void windowClosed( WindowEvent e )

 {

 }

 

 public void windowClosing( WindowEvent e )

 {

    System.exit(0);

 }

 

 public void windowDeactivated( WindowEvent e )

 {

 }

 

 public void windowDeiconified( WindowEvent e )

 {

 }

 

 public void windowOpened( WindowEvent e )

 {

 }

 

 public void windowIconified( WindowEvent e )

 {

 }

}

 

 


public class Person

{

 

  private String name;

  private String phone;

 

  Person(String n, String p)

  {

    name = n;

    phone = p;

  }

 

  public String getName()

  {

    return name;

  }

 

  public String getPhone()

  {

    return phone;

  }

 

  public void setName(String n)

  {

    name = n;

  }

 

  public void setPhone(String p)

  {

    phone = p;

  }

}


/* Written by John Avitabile

   Session 5 Example 4

   Using the Person2 class

   Person2 class object contains the Components!

*/

 

import java.util.ArrayList;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class ex4 extends JFrame implements ActionListener, WindowListener

{

 

 private JButton first = new JButton("First");

 private JButton previous = new JButton("Previous");

 private JButton next = new JButton("Next");

 private JButton last = new JButton("Last");

 private JLabel position = new JLabel();

 private int index = -1;

 private static ArrayList data = new ArrayList();

 private static Person2 current;

 

 public static void main(String args[])

 {

   ex4 myFrame = new ex4("Phone Book");

   myFrame.addWindowListener(myFrame);

 }

 

 ex4(String s)

 {

  super(s);

  setBounds(100, 100, 510, 400);

  assignData();

  setup();

  show();

 }

 

 public void assignData()

 {

   Container c = getContentPane();

   data.add(new Person2("Mary", "222-2222", c));

   data.add(new Person2("Joe", "333-3333", c));

   data.add(new Person2("Alice", "444-4444", c));

   data.add(new Person2("Sam", "555-5555", c));

 }

 

 

 


 public void setup()

 {

  Container c = getContentPane();

  // set layout manager

  c.add(first);

  c.add(previous);

  c.add(next);

  c.add(last);

  c.add(position);

  first.setBounds(10, 330, 90, 30);

  previous.setBounds(110, 330, 90, 30);

  next.setBounds(210, 330, 90, 30);

  last.setBounds(310, 330, 90, 30);

  position.setBounds(410, 330, 90, 30);

  first.addActionListener(this);   

  previous.addActionListener(this);   

  next.addActionListener(this);   

  last.addActionListener(this);

  if (data.size() > 0)

    index = 0;

  position.setText((index+1) + "/" + data.size());

  if (index >= 0)

  {

    current = (Person2) data.get(index);

    current.setup();

  }

 

 }

 

 public void actionPerformed(ActionEvent e)

 {

  if (e.getSource() == first)

  {

    index = 0;

  }

  else if (e.getSource() == previous)

  {

    if (index > 0)

      index--;

  }

  else if (e.getSource() == next)

  {

    if (index + 1 < data.size())

      index++;

  }

  else if (e.getSource() == last)

  {

    index = data.size()-1;

  }

  if (current != null)

  {

    current.remove();

    current = (Person2) data.get(index);

    current.setup(); 

    position.setText((index+1) + "/" + data.size());

  }

 }

 

 public void windowActivated( WindowEvent e )

 {

 }

 

 public void windowClosed( WindowEvent e )

 {

 }

 

 public void windowClosing( WindowEvent e )

 {

    System.exit(0);

 }

 

 public void windowDeactivated( WindowEvent e )

 {

 }

 

 public void windowDeiconified( WindowEvent e )

 {

 }

 

 public void windowOpened( WindowEvent e )

 {

 }

 

 public void windowIconified( WindowEvent e )

 {

 }

}


 

import java.awt.Container;

import javax.swing.*;

 

public class Person2

{

 

 protected JLabel nameLabel = new JLabel("Name");

 protected JLabel phoneLabel = new JLabel("Phone");

 protected JTextField nameField = new JTextField();

 protected JTextField phoneField = new JTextField();

 protected Container myContainer;

 

  Person2(String n, String p, Container c)

  {

    myContainer = c;

    nameField.setText(n);

    phoneField.setText(p);

    c.add(nameLabel);

    c.add(nameField);

    c.add(phoneLabel);

    c.add(phoneField); 

    nameLabel.setBounds(10, 30, 150, 30);

    nameField.setBounds(170, 30, 150, 30);

    phoneLabel.setBounds(10, 80, 150, 30);

    phoneField.setBounds(170, 80, 150, 30);

    phoneLabel.setVisible(false);

    nameLabel.setVisible(false);

    phoneField.setVisible(false);

    nameField.setVisible(false);

  }

 

  public String getName()

  {

    return nameField.getText();

  }

 

  public String getPhone()

  {

    return phoneField.getText();

  }

 

  public void setName(String n)

  {

    nameField.setText(n);

  }

 

  public void setPhone(String p)

  {

    phoneField.setText(p);

  }

 

 


  public void setup()

  {

    phoneLabel.setVisible(true);

    nameLabel.setVisible(true);

    phoneField.setVisible(true);

    nameField.setVisible(true);

  }

 

  public void remove()

  {

    phoneLabel.setVisible(false);

    nameLabel.setVisible(false);

    phoneField.setVisible(false);

    nameField.setVisible(false);

  }

 

  public void changeContainer(Container c)

  {

    myContainer.remove(phoneLabel);

    myContainer.remove(nameLabel);

    myContainer.remove(phoneField);

    myContainer.remove(nameField);

    myContainer = c;

    myContainer.add(phoneLabel);

    myContainer.add(nameLabel);

    myContainer.add(phoneField);

    myContainer.add(nameField);   

  }

 

}

 


 

/* Written by John Avitabile

   Session 5 Example 5

   Using the Person2, Relative and Friend classes

   Notice the Polymorphism

*/

 

import java.util.ArrayList;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class ex5 extends JFrame implements ActionListener, WindowListener

{

 

 private JButton first = new JButton("First");

 private JButton previous = new JButton("Previous");

 private JButton next = new JButton("Next");

 private JButton last = new JButton("Last");

 private JLabel position = new JLabel();

 private int index = -1;

 private static ArrayList data = new ArrayList();

 private static Person2 current;

 

 public static void main(String args[])

 {

   ex5 myFrame = new ex5("Phone Book");

   myFrame.addWindowListener(myFrame);

 }

 

 ex5(String s)

 {

  super(s);

  setBounds(100, 100, 510, 400);

  assignData();

  setup();

  show();

 }

 

 public void assignData()

 {

   Container c = getContentPane();

   data.add(new Person2("Mary", "222-2222", c));

   data.add(new Person2("Joe", "333-3333", c));

   data.add(new Relative("Judy", "666-6666", "Spouse", c));

   data.add(new Relative("Jackie", "666-6666", "Child", c));

   data.add(new Friend("Adam", "777-7777", "Male", "Old", c));

   data.add(new Person2("Alice", "444-4444", c));

   data.add(new Person2("Sam", "555-5555", c));

   data.add(new Relative("Tess", "666-6666", "Child", c));

   data.add(new Friend("Eve", "888-8888", "Female", "New", c));

 

 }

 

 


public void setup()

 {

  Container c = getContentPane();

  // set layout manager

  c.setLayout(null);

  c.add(first);

  c.add(previous);

  c.add(next);

  c.add(last);

  c.add(position);

  first.setBounds(10, 330, 90, 30);

  previous.setBounds(110, 330, 90, 30);

  next.setBounds(210, 330, 90, 30);

  last.setBounds(310, 330, 90, 30);

  position.setBounds(410, 330, 90, 30);

  first.addActionListener(this);   

  previous.addActionListener(this);   

  next.addActionListener(this);   

  last.addActionListener(this);

  if (data.size() > 0)

    index = 0;

  position.setText((index+1) + "/" + data.size());

  if (index >= 0)

  {

    current = (Person2) data.get(index);

    current.setup();

  }

 }

 

 public void actionPerformed(ActionEvent e)

 {

  if (e.getSource() == first)

  {

    index = 0;

  }

  else if (e.getSource() == previous)

  {

    if (index > 0)

      index--;

  }

  else if (e.getSource() == next)

  {

    if (index + 1 < data.size())

      index++;

  }

  else if (e.getSource() == last)

  {

    index = data.size()-1;

  }

  if (current != null)

  {

    current.remove();

    current = (Person2) data.get(index);

    current.setup(); 

    position.setText((index+1) + "/" + data.size());

  }

 }


 public void windowActivated( WindowEvent e )

 {

 }

 

 public void windowClosed( WindowEvent e )

 {

 }

 

 public void windowClosing( WindowEvent e )

 {

    System.exit(0);

 }

 

 public void windowDeactivated( WindowEvent e )

 {

 }

 

 public void windowDeiconified( WindowEvent e )

 {

 }

 

 public void windowOpened( WindowEvent e )

 {

 }

 

 public void windowIconified( WindowEvent e )

 {

 }

}

 


import java.awt.Container;

import javax.swing.*;

 

public class Relative extends Person2

{

 

 private JLabel relativeLabel = new JLabel("Type");

 private JTextField relativeField = new JTextField();

 

  Relative(String n, String p, String t, Container c)

  {

    super(n, p, c);

    relativeField.setText(t);

    c.add(relativeField);

    c.add(relativeLabel);

    relativeLabel.setBounds(10, 130, 150, 30);

    relativeField.setBounds(170, 130, 150, 30);

    relativeLabel.setVisible(false);

    relativeField.setVisible(false);

  }

 

  public String getRelative()

  {

    return relativeField.getText();

  }

 

  public void setRelative(String n)

  {

    relativeField.setText(n);

  }

 

  public void setup()

  {

    super.setup();

    relativeLabel.setVisible(true);

    relativeField.setVisible(true);

  }

 

  public void remove()

  {

    super.remove();

    relativeLabel.setVisible(false);

    relativeField.setVisible(false);

  }

 

  public void changeContainer(Container c)

  {

    super.changeContainer(c);

    myContainer.remove(relativeLabel);

    myContainer.remove(relativeField);

    myContainer = c;

    myContainer.add(relativeLabel);

    myContainer.add(relativeField);   

  }

 

}

 


import java.awt.Container;

import javax.swing.*;

 

public class Friend extends Person2

{

 

 private JLabel genderLabel = new JLabel("Gender");

 private JTextField genderField = new JTextField();

 private JLabel statusLabel = new JLabel("Status");

 private JTextField statusField = new JTextField();

 

  Friend(String n, String p, String g, String s, Container c)

  {

    super(n, p, c);

    genderField.setText(g);

    c.add(genderField);

    c.add(genderLabel);

    genderLabel.setBounds(10, 130, 150, 30);

    genderField.setBounds(170, 130, 150, 30);

    genderLabel.setVisible(false);

    genderField.setVisible(false);

    statusField.setText(s);

    c.add(statusField);

    c.add(statusLabel);

    statusLabel.setBounds(10, 180, 150, 30);

    statusField.setBounds(170, 180, 150, 30);

    statusLabel.setVisible(false);

    statusField.setVisible(false);

  }

 

  public String getStatus()

  {

    return statusField.getText();

  }

 

  public void setStatus(String n)

  {

    statusField.setText(n);

  }

 

  public String getGender()

  {

    return genderField.getText();

  }

 

  public void setGender(String n)

  {

    genderField.setText(n);

  }

 

 


  public void setup()

  {

    super.setup();

    genderLabel.setVisible(true);

    genderField.setVisible(true);

    statusLabel.setVisible(true);

    statusField.setVisible(true);

  }

 

  public void remove()

  {

    super.remove();

    genderLabel.setVisible(false);

    genderField.setVisible(false);

    statusLabel.setVisible(false);

    statusField.setVisible(false);

  }

 

  public void changeContainer(Container c)

  {

    super.changeContainer(c);

    myContainer.remove(genderLabel);

    myContainer.remove(statusLabel);

    myContainer.remove(genderField);

    myContainer.remove(statusField);

    myContainer = c;

    myContainer.add(genderLabel);

    myContainer.add(statusLabel);

    myContainer.add(genderField);

    myContainer.add(statusField);   

  }

 

}

 

 


/* Written by John Avitabile

   Session 5 Example 6

   Staring a new Frame

   JComboBox

*/

 

import java.util.ArrayList;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class ex6 extends JFrame implements ActionListener, WindowListener

{

 

 private JButton first = new JButton("First");

 private JButton previous = new JButton("Previous");

 private JButton next = new JButton("Next");

 private JButton last = new JButton("Last");

 private JButton delete = new JButton("Delete");

 private JButton insert = new JButton("Insert");

 private JLabel position = new JLabel();

 private int index = -1;

 private static ArrayList data = new ArrayList();

 private static Person2 current;

 private AddFrame add;

 

 public static void main(String args[])

 {

   ex6 myFrame = new ex6("Phone Book");

   myFrame.addWindowListener(myFrame);

 }

 

 ex6(String s)

 {

  super(s);

  setBounds(100, 100, 710, 400);

  assignData();

  setup();

  show();

 }

 

 public void assignData()

 {

   Container c = getContentPane();

   data.add(new Person2("Mary", "222-2222", c));

   data.add(new Person2("Joe", "333-3333", c));

   data.add(new Relative("Judy", "666-6666", "Spouse", c));

   data.add(new Relative("Jackie", "666-6666", "Child", c));

   data.add(new Friend("Adam", "777-7777", "Male", "Old", c));

   data.add(new Person2("Alice", "444-4444", c));

   data.add(new Person2("Sam", "555-5555", c));

   data.add(new Relative("Tess", "666-6666", "Child", c));

   data.add(new Friend("Eve", "888-8888", "Female", "New", c));

 

 }

 

 


 public void setup()

 {

  Container c = getContentPane();

  c.setLayout(null);

  c.add(first);

  c.add(previous);

  c.add(next);

  c.add(last);

  c.add(position);

  c.add(delete);

  c.add(insert);

  first.setBounds(10, 330, 90, 30);

  previous.setBounds(110, 330, 90, 30);

  next.setBounds(210, 330, 90, 30);

  last.setBounds(310, 330, 90, 30);

  position.setBounds(610, 330, 50, 30);

  delete.setBounds(410, 330, 90, 30);

  insert.setBounds(510, 330, 90, 30);

  first.addActionListener(this);   

  previous.addActionListener(this);   

  next.addActionListener(this);   

  last.addActionListener(this);

  delete.addActionListener(this);

  insert.addActionListener(this);

  if (data.size() > 0)

    index = 0;

  position.setText((index+1) + "/" + data.size());

  if (index >= 0)

  {

    current = (Person2) data.get(index);

    current.setup();

  }

 

 }

 

 public void actionPerformed(ActionEvent e)

 {

  if (e.getSource() == first)

  {

    index = 0;

  }

  else if (e.getSource() == previous)

  {

    if (index > 0)

      index--;

  }

  else if (e.getSource() == next)

  {

    if (index + 1 < data.size())

      index++;

  }

  else if (e.getSource() == last)

  {

    index = data.size()-1;

    Person2 temp = (Person2) data.get(index);

  }

 


  else if (e.getSource() == insert)

  {

    add = new AddFrame(data, getContentPane());

    add.show();

  }

  else if (e.getSource() == delete)

  {

    data.remove(current);

    if (data.size() == index)// last got deleted

      index--;

    // otherwise, it's the same

    if (data.size() == 0)

    {

      first.setEnabled(false);

      previous.setEnabled(false);

      next.setEnabled(false);

      last.setEnabled(false);

      delete.setEnabled(false);

    }

  }

  if (current != null)

  {

    current.remove();

    if (index != -1)

    {

      current = (Person2) data.get(index);

      current.setup(); 

      position.setText((index+1) + "/" + data.size());

    }

  }

 }

 

 public void windowActivated( WindowEvent e )

 {

 }

 

 public void windowClosed( WindowEvent e )

 {

 }

 

 public void windowClosing( WindowEvent e )

 {

    System.exit(0);

 }

 

 public void windowDeactivated( WindowEvent e )

 {

 }

 

 public void windowDeiconified( WindowEvent e )

 {

 }

 

 public void windowOpened( WindowEvent e )

 {

 }

 

 public void windowIconified( WindowEvent e )

 {

 }

}

 

 

 


import java.util.ArrayList;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class AddFrame extends JFrame implements ActionListener, WindowListener, ItemListener

{

 

 private JButton accept = new JButton("Accept");

 private JButton reject = new JButton("Reject");

 private String[] peopleTypes = {"Person", "Friend", "Relative"};

 private JComboBox choices = new JComboBox(peopleTypes);

 private Person2 newPerson;

 

 private ArrayList data;

 private Person2 current;

 private Container parent;

 

 AddFrame(ArrayList d, Container p)

 {

  super("Add Person");

  parent = p;

  data = d;

  setBounds(0, 0, 500, 400);

  setup();

  Container c = getContentPane();

  newPerson = new Person2("","",c);

  newPerson.setup();

 }

 

 public void setup()

 {

  Container c = getContentPane();

  c.setLayout(null);

  c.add(accept);

  c.add(reject);

  c.add(choices);

  accept.setBounds(10, 330, 90, 30);

  reject.setBounds(110, 330, 90, 30);

  choices.setBounds(210, 330, 90, 30);

  accept.addActionListener(this);   

  reject.addActionListener(this);

  choices.addItemListener(this);

 }

 

 


 public void actionPerformed(ActionEvent e)

 {

  if (e.getSource() == accept)

  {

    data.add(newPerson);

    newPerson.remove();

    newPerson.changeContainer(parent);

    switch (choices.getSelectedIndex())

    {

      case 0:

        newPerson = new Person2("", "", getContentPane());

        break;

      case 1:

        newPerson = new Friend("", "", "", "", getContentPane());

        break;

      case 2:

        newPerson = new Relative("", "", "", getContentPane());

        break;

      }

      newPerson.setup();

  }

  else if (e.getSource() == reject)

  {

    newPerson.remove();

    switch (choices.getSelectedIndex())

    {

      case 0:

        newPerson = new Person2("", "", getContentPane());

        break;

      case 1:

        newPerson = new Friend("", "", "", "", getContentPane());

        break;

      case 2:

        newPerson = new Relative("", "", "", getContentPane());

        break;

      }

      newPerson.setup();

  }

 }

 

 


 public void itemStateChanged(ItemEvent e )

 {

   if (e.getItemSelectable() == choices)

   {

      newPerson.remove();

      switch (choices.getSelectedIndex())

      {

        case 0:

          newPerson = new Person2("", "", getContentPane());

          break;

        case 1:

          newPerson = new Friend("", "", "", "", getContentPane());

          break;

        case 2:

          newPerson = new Relative("", "", "", getContentPane());

          break;

      }

      newPerson.setup();

   }

 }

 

 

 

 public void windowActivated( WindowEvent e )

 {

 }

 

 public void windowClosed( WindowEvent e )

 {

 }

 

 

 public void windowClosing( WindowEvent e )

 {

 }

 

 public void windowDeactivated( WindowEvent e )

 {

 }

 

 public void windowDeiconified( WindowEvent e )

 {

 }

 

 public void windowOpened( WindowEvent e )

 {

 }

 

 public void windowIconified( WindowEvent e )

 {

 }

}