public class Contact
{
private String firstName;
private String lastName;
private String mi;
private String phoneNumber;
private String email;
private String street;
private String city;
private String state;
private String zip;
private String birthday;
public Contact()
{
this(null, null, null, null, null, null, null, null, null, null);
}
public Contact(String fn, String ln)
{
firstName = fn;
lastName = ln;
}
public Contact(String fn, String ln, String m, String pn, String em, String str, String cty, String st, String zp, String bd)
{
firstName = fn;
lastName = ln;
mi = m;
phoneNumber = pn;
email = em;
street = str;
city = cty;
state = st;
zip = zp;
birthday = bd;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getMi()
{
return mi;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public String getEmail()
{
return email;
}
public String getStreet()
{
return street;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public String getZip()
{
return zip;
}
public String getBirthday()
{
return birthday;
}
public void setFirstName(String fn)
{
firstName = fn;
}
public void seLastName(String ln)
{
lastName = ln;
}
public void setMi(String m)
{
mi = m;
}
public void setPhoneNumber(String pn)
{
phoneNumber = pn;
}
public void setEmail(String em)
{
email = em;
}
public void setBirthday(String bd)
{
birthday = bd;
}
public String toString()
{
return firstName + " " + lastName;
}
} //end class
public class Node
{
private Contact contact;
public Node next;
public Node()
{
contact = null;
next = null;
}
public Node(Contact c)
{
setContact(c);
setNext(null);
}
public Contact getContact()
{
return contact;
}
public Node getNext()
{
return next;
}
public void setContact(Contact c)
{
contact = c;
}
public void setNext(Node node)
{
next = node;
}
public String toString()
{
return contact.toString();
}
} //end class
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class AddressBook2
{
JFrame frame1, frame2;
//frame1 variables
private JButton addButton;
private JButton editButton;
private JButton removeButton;
private JButton sortFirstButton;
private JButton sortLastButton;
private JButton exitButton;
private JList contactList;
private DefaultListModel listModel;
//frame2 variables
private JTextField fnameField;
private JTextField lnameField;
private JTextField miField;
private JTextField pnField;
private JTextField stateField;
private JTextField cityField;
private JTextField streetField;
private JTextField zipField;
private JTextField bdField;
private JTextField emailField;
private JButton saveButton;
Node front, q, r;
public AddressBook2()
{
frame1 = new JFrame("My Contacts");
frame2 = new JFrame("Contact");
frame1.setBounds(0, 0, 450, 500);
frame2.setBounds(450, 0, 450, 500);
listModel = new DefaultListModel(); //Pass the listModel to the list, then manipulate the model instead of the list.
JPanel frame1TopPanel = new JPanel(); //default is flowLayout
JPanel frame1MiddlePanel = new JPanel(new GridLayout(0, 1));
frame1MiddlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
JPanel frame1BottomPanel = new JPanel(new FlowLayout());
addButton = new JButton("Add");
editButton = new JButton("Edit");
removeButton = new JButton("Remove");
//create and configure list
contactList = new JList(listModel);
contactList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
contactList.setSelectedIndex(0);
//contactList.addListSelectionListener(listListener);
contactList.setVisibleRowCount(15);
JScrollPane contactListScrollPane = new JScrollPane(contactList);
//add buttons to frame1TopPanel
frame1TopPanel.add(addButton);
frame1TopPanel.add(editButton);
frame1TopPanel.add(removeButton);
//add list to middle panel
frame1MiddlePanel.add(contactListScrollPane);
//create bottom buttons and add them to frame1BottomPanel
exitButton = new JButton("Exit");
sortFirstButton = new JButton("Sort by First Name");
sortLastButton = new JButton("Sort by Last Name");
frame1BottomPanel.add(sortFirstButton);
frame1BottomPanel.add(sortLastButton);
frame1BottomPanel.add(exitButton);
//Now the frame2 GUI
JPanel frame2Panel = new JPanel();
Font labelFont = new Font("Courier", Font.BOLD, 12);
//make a label for each frame1 text field; add labels and text fields to frame2Panel.
JLabel fnameLabel = new JLabel();
fnameLabel.setFont(labelFont);
fnameLabel.setText(" First Name:");
fnameField = new JTextField(20);
frame2Panel.add(fnameLabel);
frame2Panel.add(fnameField);
JLabel lnameLabel = new JLabel();
lnameLabel.setFont(labelFont);
lnameLabel.setText(" Last Name:");
lnameField = new JTextField(20);
frame2Panel.add(lnameLabel);
frame2Panel.add(lnameField);
JLabel miLabel = new JLabel();
miLabel.setFont(labelFont);
miLabel.setText("Middle Initial:");
miField = new JTextField(20);
frame2Panel.add(miLabel);
frame2Panel.add(miField);
JLabel pnLabel = new JLabel();
pnLabel.setFont(labelFont);
pnLabel.setText(" Phone Number:");
pnField = new JTextField(20);
frame2Panel.add(pnLabel);
frame2Panel.add(pnField);
JLabel emailLabel = new JLabel();
emailLabel.setFont(labelFont);
emailLabel.setText(" Email:");
emailField = new JTextField(20);
frame2Panel.add(emailLabel);
frame2Panel.add(emailField);
JLabel streetLabel = new JLabel();
streetLabel.setFont(labelFont);
streetLabel.setText(" Street:");
streetField = new JTextField(20);
frame2Panel.add(streetLabel);
frame2Panel.add(streetField);
JLabel cityLabel = new JLabel();
cityLabel.setFont(labelFont);
cityLabel.setText(" City:");
cityField = new JTextField(20);
frame2Panel.add(cityLabel);
frame2Panel.add(cityField);
JLabel stateLabel = new JLabel();
stateLabel.setFont(labelFont);
stateLabel.setText(" State:");
stateField = new JTextField(20);
frame2Panel.add(stateLabel);
frame2Panel.add(stateField);
JLabel zipLabel = new JLabel();
zipLabel.setFont(labelFont);
zipLabel.setText(" Zip:");
zipField = new JTextField(20);
frame2Panel.add(zipLabel);
frame2Panel.add(zipField);
JLabel bdLabel = new JLabel();
bdLabel.setFont(labelFont);
bdLabel.setText(" Birthday:");
bdField = new JTextField(20);
frame2Panel.add(bdLabel);
frame2Panel.add(bdField);
frame2.add(frame2Panel, BorderLayout.CENTER);
//create the frame2 buttons, add them to a new panel, and place the panel on the frame
saveButton = new JButton("Save");
JPanel frame2ButtonPanel = new JPanel();
frame2ButtonPanel.add(saveButton);
//register a listener with each button
addButton.addActionListener(new ButtonListener());
editButton.addActionListener(new ButtonListener());
removeButton.addActionListener(new ButtonListener());
sortFirstButton.addActionListener(new ButtonListener());
sortLastButton.addActionListener(new ButtonListener());
exitButton.addActionListener(new ButtonListener());
saveButton.addActionListener(new ButtonListener());
frame1.add(frame1TopPanel, BorderLayout.NORTH);
frame1.add(frame1MiddlePanel, BorderLayout.CENTER);
frame1.add(frame1BottomPanel, BorderLayout.SOUTH);
frame1.setResizable(false);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(frame2Panel, BorderLayout.CENTER);
frame2.add(frame2ButtonPanel, BorderLayout.SOUTH);
frame2.setResizable(false);
frame2.setVisible(false);
frame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//node stuff--This is the bad part.
Contact dummyContact;
dummyContact = new Contact("Sam", "Spade");
front = new Node(dummyContact); //If I don't do this my code will not work because front will be null, and then I can't loop through to edit/remove--need to figure it out
listModel.addElement(front);
q = front; //both q and front now reference the first node, which has: data = null and next = null.
//add contents of linked list to listModel
Node s = front;
}
private class ButtonListener implements ActionListener
{
String firstName;
String lastName;
String mi;
String phoneNumber;
String email;
String street;
String city;
String state;
String zip;
String birthday;
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == saveButton)
{
firstName = fnameField.getText();
lastName = lnameField.getText();
mi = miField.getText();
phoneNumber = pnField.getText();
email = emailField.getText();
street = streetField.getText();
city = cityField.getText();
state = stateField.getText();
zip = zipField.getText();
birthday = bdField.getText();
Contact c = new Contact(firstName, lastName, mi, phoneNumber, email, street, city, state, zip, birthday);
r = new Node(c);
listModel.addElement(r);
q.next = r;
q = r;
frame2.setVisible(false);
addButton.setEnabled(true);
editButton.setEnabled(true);
removeButton.setEnabled(true);
sortFirstButton.setEnabled(true);
sortLastButton.setEnabled(true);
saveButton.setEnabled(true);
//clear all frame2 fields for next use
fnameField.setText("");
lnameField.setText("");
miField.setText("");
pnField.setText("");
emailField.setText("");
streetField.setText("");
cityField.setText("");
stateField.setText("");
zipField.setText("");
bdField.setText("");
}
else if (e.getSource() == addButton)
{
frame2.setVisible(true);
addButton.setEnabled(false);
editButton.setEnabled(false);
removeButton.setEnabled(false);
sortFirstButton.setEnabled(false);
sortLastButton.setEnabled(false);
saveButton.setEnabled(true);
}
else if (e.getSource() == editButton)
{
frame2.setVisible(true);
addButton.setEnabled(false);
editButton.setEnabled(false);
removeButton.setEnabled(false);
sortFirstButton.setEnabled(false);
sortLastButton.setEnabled(false);
int index = contactList.getSelectedIndex();
Node p = (Node)listModel.getElementAt(index);
String name = p.toString();
fnameField.setText(p.getContact().getFirstName());
lnameField.setText(p.getContact().getLastName());
miField.setText(p.getContact().getMi());
pnField.setText(p.getContact().getPhoneNumber());
stateField.setText(p.getContact().getState());
cityField.setText(p.getContact().getCity());
streetField.setText(p.getContact().getStreet());
zipField.setText(p.getContact().getZip());
bdField.setText(p.getContact().getBirthday());
emailField.setText(p.getContact().getEmail());
Node d = front;
Node prev = null;
while (d != null && !name.equals(d.toString()))
{
prev = d;
d = d.getNext();
}
if (d == front)
{
front = front.getNext();
}
else
{
prev.setNext(d.getNext()); //cut the old node out of chain
}
listModel.remove(index);
//Now that the old contact is deleted, it should get replaced with the edited version when the "save" button is pressed.
}
else if (e.getSource() == sortFirstButton) //problems with this
{
sortByFirst(front);
//now the list should be sorted. Clear JList of unsorted data (is there any way to make the listModel actually reflect the order of the data?).
listModel.clear();
Node j = front;
while (j != null)
{
listModel.addElement(j);
j = j.getNext();
}
}
else if (e.getSource() == sortLastButton) //this too
{
sortByLast(front);
listModel.clear();
Node j = front;
while (j != null)
{
listModel.addElement(j);
j = j.getNext();
}
}
else if (e.getSource() == removeButton)
{
int index = contactList.getSelectedIndex();
Node p = (Node)listModel.getElementAt(index);
String name = p.toString();
Node d = front;
Node prev = null;
while (d != null && !name.equals(d.toString()))
{
prev = d;
d = d.getNext();
}
if (d == front)
{
front = front.getNext();
}
else
{
prev.setNext(d.getNext()); //cut the old node out of chain
}
listModel.remove(index);
}
else if (e.getSource() == exitButton)
{
System.exit(0);
}
}
}
public static void main(String[] args)
{
AddressBook2 frames = new AddressBook2();
}
private static void sortByFirst(Node front)
{
//Enter loop only if there are elements in list
boolean swapped = (front != null);
// Only continue loop if a swap is made
while (swapped)
{
swapped = false;
// Maintain pointers
Node curr = front;
Node next = curr.getNext();
Node prev = null;
// Cannot swap last element with its next
while (next != null)
{
// swap if items in wrong order
if (curr.getContact().getFirstName().compareTo(next.getContact().getFirstName()) > 0)
{
// notify loop to do one more pass
swapped = true;
// swap elements (swapping head in special case
if (curr == front)
{
front = next;
Node temp = next.getNext();
next.setNext(curr);
curr.setNext(temp);
curr = front;
}
else
{
prev.setNext(curr.getNext());
curr.setNext(next.getNext());
next.setNext(curr);
curr = next;
}
}
// move to next element
prev = curr;
curr = curr.getNext();
next = curr.getNext();
//print the linked list to check elements
Node p = front;
while (p != null)
{
System.out.println(p.getContact().toString());
p = p.getNext();
}
System.out.println();
}
}
}
private static void sortByLast(Node front)
{
//Enter loop only if there are elements in list
boolean swapped = (front != null);
// Only continue loop if a swap is made
while (swapped)
{
swapped = false;
// Maintain pointers
Node curr = front;
Node next = curr.getNext();
Node prev = null;
// Cannot swap last element with its next
while (next != null)
{
// swap if items in wrong order
if (curr.getContact().getLastName().compareTo(next.getContact().getLastName()) > 0)
{
// notify loop to do one more pass
swapped = true;
// swap elements (swapping head in special case
if (curr == front)
{
front = next;
Node temp = next.getNext();
next.setNext(curr);
curr.setNext(temp);
curr = front;
}
else
{
prev.setNext(curr.getNext());
curr.setNext(next.getNext());
next.setNext(curr);
curr = next;
}
}
// move to next element
prev = curr;
curr = curr.getNext();
next = curr.getNext();
}
}
}
}