This is the current code that i have working for my address book. Everything works except for the DeteleAction, SaveAction, and OpenAction. If anyone could reply with some help sometime today it would be greatly appreciated that so i can hand something in on time for once, lol...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javabook.*;
import java.awt.event.ActionEvent;
publicclass AddressBook
{
// Use these class variables to store values for searching
privatestatic Person person[] = new Person[50];
JTextField lastName;
JTextField firstName;
JTextField phone;
JTextField email;
JTextField city;
JTextField state;
JTextField zip;
privateint i;
public AddressBook()
{
buildGui();
} // end AddressBook constructor
public Component buildGui()
{
final JLabel firstLabel = new JLabel("First Name");
final JLabel lastLabel = new JLabel("Last Name");
final JLabel phoneLabel = new JLabel("Phone Number");
final JLabel emailLabel = new JLabel("E-mail Address");
final JLabel cityLabel = new JLabel("City");
final JLabel stateLabel = new JLabel("State");
final JLabel zipLabel = new JLabel("Zip");
final JButton add = new JButton(new AddAction());
final JButton delete = new JButton(new DeleteAction());
final JButton search = new JButton(new SearchAction());
final JButton save = new JButton(new SaveAction());
final JButton open = new JButton(new OpenAction());
final JButton view = new JButton("View Address Book");
firstName = new JTextField(15);
lastName = new JTextField(15);
phone = new JTextField(15);
email = new JTextField(15);
city = new JTextField(15);
state = new JTextField(15);
zip = new JTextField(15);
JPanel pane1 = new JPanel();
pane1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
pane1.setLayout(new GridLayout(10, 2, 10, 10));
pane1.add(firstLabel);
pane1.add(firstName);
pane1.add(lastLabel);
pane1.add(lastName);
pane1.add(phoneLabel);
pane1.add(phone);
pane1.add(emailLabel);
pane1.add(email);
pane1.add(cityLabel);
pane1.add(city);
pane1.add(stateLabel);
pane1.add(state);
pane1.add(zipLabel);
pane1.add(zip);
pane1.add(add);
pane1.add(delete);
pane1.add(search);
pane1.add(view);
pane1.add(save);
pane1.add(open);
return pane1;
// put addressbook gui componenets in here
// create frame/panels etc..
// example
} // end buildGui
publicstaticvoid main(String args[])
{
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e)
{
}
//Create the top-level container and add contents to it.
JFrame frame = new JFrame("AddressBook");
AddressBook app = new AddressBook();
Component contents = app.buildGui();
frame.getContentPane().add(contents, BorderLayout.CENTER);
//Finish setting up the frame, and show it.
frame.addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
} // end main method
privateint search(String s)
{
int i= 0;
//put search array here - return the value of index of found item or null address
while (person[i] != null && !person[i].getLastName().equals(s))
{
i++;
}
return i;
}
privateclass SearchAction extends AbstractAction
{
public SearchAction()
{
putValue(NAME, "Search");
putValue(SHORT_DESCRIPTION, "Search");
}
publicvoid actionPerformed(ActionEvent e)
{
i = search(lastName.getText());
if(person[i] == null)
{
JOptionPane.showMessageDialog(null, lastName.getText() + " Person Not Found");
lastName.setText(" ");
}
else
{
firstName.setText(person[i].getFirstName());
lastName.setText(person[i].getLastName());
phone.setText(person[i].getPhone());
email.setText(person[i].getEmail());
city.setText(person[i].getCity());
state.setText(person[i].getState());
phone.setText(person[i].getPhone());
zip.setText(person[i].getZip());
}
}
}
privateclass AddAction extends AbstractAction
{
public AddAction()
{
putValue(NAME, "Add");
putValue(SHORT_DESCRIPTION, "Add");
}
publicvoid actionPerformed(ActionEvent e)
{
int i = 0;
while (person[i] != null)
i++;
if (person[i] == null)
{
person[i] = new Person(firstName.getText(),lastName.getText(),phone.getText(),
email.getText(),city.getText(),state.getText(),zip.getText());
firstName.setText(" ");
lastName.setText(" ");
phone.setText(" ");
email.setText(" ");
city.setText(" ");
state.setText(" ");
zip.setText(" ");
JOptionPane.showMessageDialog(null, person[i].getFirstName() + " " + person[i].getLastName() + " Was Added!");
i++;
}
}
}
privateclass DeleteAction extends AbstractAction
{
public DeleteAction()
{
putValue(NAME, "Delete");
putValue(SHORT_DESCRIPTION, "Delete");
}
publicvoid actionPerformed(ActionEvent e)
{
int i = 0;
i = search(lastName.getText());
while (!person[i].getLastName().equals(lastName) && person[i] != null)
i++;
if(person[i].getLastName().equals(lastName))
{
firstName.setText(" ");
lastName.setText(" ");
phone.setText(" ");
email.setText(" ");
city.setText(" ");
state.setText(" ");
zip.setText(" ");
person[i].setFirstName(null);
person[i].setLastName(null);
person[i].setEmail(null);
person[i].setCity(null);
person[i].setState(null);
person[i].setPhone(null);
person[i].setZip(null);
}
// put code for deleting here
}
}
privateclass SaveAction extends AbstractAction
{
public SaveAction()
{
putValue(NAME, "Save");
putValue(SHORT_DESCRIPTION, "Save");
}
publicvoid actionPerformed(ActionEvent e)throws IOException, ClassNotFoundException
{
MainWindow mainWindow;
mainWindow = new MainWindow("");
FileDialog fileBox = new FileDialog(mainWindow, "Save", FileDialog.SAVE);
fileBox.setVisible(true);
File outFile = new File("person.dat");
FileOutputStream outFileStream = new FileOutputStream(outFile);
ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStream);
outObjectStream.writeObject(person);
outObjectStream.close();
}
}
privateclass OpenAction extends AbstractAction
{
public OpenAction()
{
putValue(NAME, "Open");
putValue(SHORT_DESCRIPTION, "Open");
}
publicvoid actionPerformed(ActionEvent e)throws IOException, ClassNotFoundException
{
MainWindow mainWindow;
mainWindow = new MainWindow("");
FileDialog fileBox = new FileDialog(mainWindow, "Open", FileDialog.LOAD);
fileBox.setVisible(true);
File inFile = new File("person.dat");
FileInputStream inFileStream = new FileInputStream(inFile);
ObjectInputStream inObjectStream = new ObjectInputStream(inFileStream);
inObjectStream.readObject();
inObjectStream.close();
}
}
} // end AddressBook class
class Person
{
private String firstName;
private String lastName;
private String email;
private String city;
private String state;
private String phoneNumber;
private String zip;
public Person()
{
}
//My constructor for the person class
public Person(String a, String b, String c,
String e, String f, String g, String h)
{
setFirstName(a);
setLastName(b);
setEmail(c);
setCity(e);
setState(f);
setPhone(g);
setZip(h);
}
//These are my methods to try to get and send the text you enter
public String getFirstName()
{
return firstName;
}
publicvoid setFirstName(String firstNam)
{
firstName = firstNam;
}
public String getLastName()
{
return lastName;
}
publicvoid setLastName(String lastNam)
{
lastName = lastNam;
}
public String getEmail()
{
return email;
}
publicvoid setEmail(String mail)
{
email = mail;
}
public String getCity()
{
return city;
}
publicvoid setCity(String city1)
{
city = city1;
}
public String getState()
{
return state;
}
publicvoid setState(String stat)
{
state = stat;
}
public String getPhone()
{
return phoneNumber;
}
publicvoid setPhone(String phoneNumb)
{
phoneNumber = phoneNumb;
}
public String getZip()
{
return zip;
}
publicvoid setZip(String zip1)
{
zip = zip1;
}
}