i'm having a few problem with my phone book program:
1. If the any of the buttons have empty fields, I need to display an error message. How would I do that? Some sort of try/catch statement or if/else statement?
2. I'm a bit clueless as how I would go about doing the update statement.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* This program creates a phone book with contacts using swing components.
*
*/
public class PhoneBook extends JFrame implements ActionListener
{
ArrayList<String> nameList = new ArrayList<String>();
ArrayList<String> phoneList = new ArrayList<String>();
String name;
String phone;
double phoneNum;
int index;
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
JLabel nameLabel = new JLabel("Name");
JTextField nameField = new JTextField(20);
JLabel phoneLabel = new JLabel("Phone");
JTextField phoneField = new JTextField(20);
JButton addBtn = new JButton("Add");
JButton deleteBtn = new JButton("Delete");
JButton updateBtn = new JButton("Update");
JButton findBtn = new JButton("Find");
/**
* This method creates the instance of phone book and sets the layout.
*/
public PhoneBook()
{
super ("Phone Book");
Container c = getContentPane();
c.setLayout(new BorderLayout());
northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
centerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
southPanel.setLayout(new FlowLayout());
northPanel.add(nameLabel);
northPanel.add(nameField);
centerPanel.add(phoneLabel);
centerPanel.add(phoneField);
southPanel.add(addBtn);
southPanel.add(deleteBtn);
southPanel.add(updateBtn);
southPanel.add(findBtn);
c.add(northPanel,BorderLayout.NORTH);
c.add(centerPanel,BorderLayout.CENTER);
c.add(southPanel,BorderLayout.SOUTH);
addBtn.addActionListener(this);
deleteBtn.addActionListener(this);
updateBtn.addActionListener(this);
findBtn.addActionListener(this);
}
/**
* This method sets the look and feel and frame for the application.
* @param args This parameter is the one that takes the arguments.
*/
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"The UIManager couldn't setthe motif look and feel","Error",JOptionPane.ERROR_MESSAGE);
}
PhoneBook f = new PhoneBook();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,140);
f.setLocation(350,350);
f.setVisible(true);
}
/**
* This method is triggered when names and phone numbers are added to the list.
*/
public void actionPerformed(ActionEvent e)
{
index = 0;
@SuppressWarnings("unused")
boolean nameAdded;
@SuppressWarnings("unused")
boolean phoneAdded;
name = new String (nameField.getText());
phone = new String (phoneField.getText());
if(e.getSource() == addBtn)
{
nameAdded = nameList.add(name);
phoneAdded = phoneList.add(phone);
if(nameList.contains(name))
{
JOptionPane.showMessageDialog(null,"The name "+name+" & phone number "+phone+" were successfully added","Info",JOptionPane.INFORMATION_MESSAGE);
clear();
}
}
if(e.getSource() == deleteBtn)
{
if(validName())
{
int phoneIndex = getIndex();
nameList.remove(phoneIndex);
phoneList.remove(phoneIndex);
}
{
JOptionPane.showMessageDialog(null,"Name "+nameField.getText()+" was successfully removed ","Info",JOptionPane.INFORMATION_MESSAGE);
clear();
}
}
if(e.getSource() == updateBtn)
{
}
if(e.getSource() == findBtn)
{
int findIndex = getIndex();
if(findIndex < 0){
//no such name..
JOptionPane.showMessageDialog(null,"Name not found","Info",JOptionPane.INFORMATION_MESSAGE);
clear();
}
else {
//tha name was found - means a legal index
//perform that action:
phoneField.setText((String)phoneList.get(findIndex));
}
}
}
/**
* This method clears the fields.
*/
public void clear()
{
nameField.setText("");
nameField.requestFocus();
phoneField.setText("");
}
/**
* This method gets the index.
* @return Returns the name if it is in the list.
*/
public int getIndex()
{
for(int i = 0;i<nameList.size();i++)
{
if(nameField.getText().equals(nameList.get(i)))
return i;
}
JOptionPane.showMessageDialog(null,"List does not contain the name "+nameField.getText(),"Info",JOptionPane.INFORMATION_MESSAGE);
return -1;
}
/**
* This method checks to see if it is a valid name.
* @return Returns the validName (true) if the name list contains the text entered.
*/
public boolean validName()
{
boolean validName = false;
if(nameList.contains(nameField.getText()))
validName = true;
return validName;
}
}