Professional Documents
Culture Documents
Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
JFrame appFrame;
public AddressBookDemo()
{
name = "";
address = "";
email = "";
phone = -1 ; //Stores 0 to indicate no Phone Number
recordNumber = -1;
createGUI();
cPane = appFrame.getContentPane();
cPane.setLayout(new GridBagLayout());
//Arrange components on contentPane and set Action Listeners to each
JButton
arrangeComponents();
appFrame.setSize(240,300);
appFrame.setResizable(false);
appFrame.setVisible(true);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbbSave.addActionListener(this);
jbnDelete.addActionListener(this);
jbnClear.addActionListener(this);
jbnUpdate.addActionListener(this);
jbnSearch.addActionListener(this);
jbnForward.addActionListener(this);
jbnBack.addActionListener(this);
jbnExit.addActionListener(this);
}
if (e.getSource () == jbbSave){
savePerson();
clear();
}
}
// Save the Person into the Address Book
public void savePerson(){
name = jtfName.getText();
name = name.toUpperCase(); //Save all names in Uppercase
address = jtfAddress.getText();
try{
phone = Integer.parseInt(""+jtfPhone.getText());
}catch(Exception e){
/*System.out.print("Input is a string");
JOptionPane.showMessageDialog(null, "Please enter Phone
Number");*/
}
email = jtfEmail.getText();
if(name.equals("")){
JOptionPane.showMessageDialog(null, "Please enter person name.");
}else{
//create a PersonInfo object and pass it to PersonDAO to save
it
PersonInfo person = new PersonInfo(name, address, phone,
email);
pDAO.savePerson(person);
JOptionPane.showMessageDialog(null, "Person Saved");
}
}
name = jtfName.getText();
name = name.toUpperCase();
if(name.equals("")){
JOptionPane.showMessageDialog(null,"Please enter person name to
delete.");
}
else{
//remove Person of the given name from the Address Book database
int numberOfDeleted = pDAO.removePerson(name);
JOptionPane.showMessageDialog(null, numberOfDeleted + " Record(s)
deleted.");
}
}
int id = person.getId();
name = jtfName.getText();
name = name.toUpperCase();
/*clear contents of arraylist if there are any from previous search*/
personsList.clear();
recordNumber = 0;
if(name.equals("")){
JOptionPane.showMessageDialog(null,"Please enter person name to
search.");
}
else{
/*get an array list of searched persons using PersonDAO*/
personsList = pDAO.searchPerson(name);
if(personsList.size() == 0){
JOptionPane.showMessageDialog(null, "No records found.");
//Perform a clear if no records are found.
clear();
}
else
{
/*downcast the object from array list to PersonInfo*/
PersonInfo person = (PersonInfo)
personsList.get(recordNumber);
/*if user has reached the end of results, disable forward button*/
jbnForward.setEnabled(false);
jbnBack.setEnabled(true);
if(recordNumber < 0 ){
JOptionPane.showMessageDialog(null, "You have reached begining " +
"of search results");
jtfName.setText("");
jtfAddress.setText("");
jtfPhone.setText("");
jtfEmail.setText("");