Hi on my Open & Save buttons, I am trying to get them to work I have really not a clue how to make a JFileChooser Dialog box. Right now I am kind of winging it but not getting anywhere. In my ContactList class on my open method I am getting a java:303 FileNotFoundException Error; and it is saying that is must be caught or declared to be thrown. Could someone help me fix it or tell me that I am way off. And also if I am close to being correct would I just create that for the Save except use "ShowSaveDialog" instead of the "ShowOpenDialog".
CODE
import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileReader;
import java.io.File;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class ContactList extends JPanel implements ActionListener
{
protected JButton b1, b2, b3, b4, b5, b6, b7;
ArrayList<Contact> list;
Scanner scan;
FileReader open;
public ContactList()
{
list = new ArrayList<Contact>();
scan = new Scanner(System.in);
b1 = new JButton("Add");
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("Add");
b2 = new JButton("Delete");
b2.setMnemonic(KeyEvent.VK_D);
b2.setActionCommand("Delete");
b3 = new JButton("Modify");
b3.setMnemonic(KeyEvent.VK_D);
b3.setActionCommand("Modify");
b4 = new JButton("Display All");
b4.setMnemonic(KeyEvent.VK_D);
b4.setActionCommand("DisplayAll");
b5 = new JButton("Display a specific");
b5.setMnemonic(KeyEvent.VK_D);
b5.setActionCommand("DisplayI");
b6 = new JButton("Save");
b6.setMnemonic(KeyEvent.VK_D);
b6.setActionCommand("Save");
b7 = new JButton("Open");
b7.setMnemonic(KeyEvent.VK_D);
b7.setActionCommand("Open");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b1.setToolTipText("Click this button to add a contact.");
b2.setToolTipText("Click this button to remove a contact.");
b3.setToolTipText("Click this button to modify a contact.");
b4.setToolTipText("Click this button to display all contacts.");
b5.setToolTipText("Click this button to display a specific contact.");
b6.setToolTipText("Click this button to print all contacts to a file.");
b7.setToolTipText("Click this button to open a file.");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
}
public void actionPerformed(ActionEvent e)
{
if ("Add".equals(e.getActionCommand()))
{
list.add(inputContact(scan));
System.out.println("All Contacts");
System.out.println();
for(int i = 0; i < list.size(); i++)
{
Contact x = list.get(i);
System.out.println("First Name "+x.getNameFL());
System.out.println("Last Name "+x.getNameLF());
System.out.println("Affiliation "+x.getAff());
System.out.println("Home Phone "+x.getHome());
System.out.println("Cell Phone "+x.getCell());
System.out.println("Preferred Phone "+x.getPref());
System.out.println("Home E-mail "+x.getEmailH());
System.out.println("Work E-mail "+x.getEmailW());
System.out.println("Preferred E-mail "+x.getEmailP());
System.out.println("url "+x.getU());
System.out.println("Comment "+x.getCom());
}
}
else if ("Modify".equals(e.getActionCommand()))
{
System.out.println("Which contact would you like to modify? (Pick number)");
for(int i = 0; i < list.size(); i++)
{
Contact x = list.get(i);
System.out.println(i+1+" "+x.getNameFL()+" "+x.getNameLF());
}
int choice = scan.nextInt();
Contact c = list.get(choice-1);
System.out.println("What field do you want to modify? Please time one");
System.out.println("First Name, Last Name, Affiliation, Home Phone, Cell Phone");
System.out.println("Preferred phone, Home Email, Work Email, preferred Email, url, comment");
String select = scan.next();
if (select.equalsIgnoreCase("FirstName"))
{
System.out.println("Please type in First Name");
String ans = scan.next();
c.setNameFL(ans);
}
else if (select.equalsIgnoreCase("LastName"))
{
System.out.println("Please type in Last Name");
String ans = scan.next();
c.setNameLF(ans);
}
else if (select.equalsIgnoreCase("Affiliation"))
{
System.out.println("Please type in Affiliation");
String ans = scan.next();
c.setAff(ans);
}
else if (select.equalsIgnoreCase("Homephone"))
{
System.out.println("Please type in Home phone");
String ans = scan.next();
c.setHome(ans);
}
else if (select.equalsIgnoreCase("Cellphone"))
{
System.out.println("Please type in Cell phone");
String ans = scan.next();
c.setCell(ans);
}
else if (select.equalsIgnoreCase("Preferredphone"))
{
System.out.println("Please type in Preferred phone");
String ans = scan.next();
c.setPref(ans);
}
else if (select.equalsIgnoreCase("HomeEmail"))
{
System.out.println("Please type in Home Email");
String ans = scan.next();
c.setEmailH(ans);
}
else if (select.equalsIgnoreCase("WorkEmail"))
{
System.out.println("Please type in Work Email");
String ans = scan.next();
c.setEmailW(ans);
}
else if (select.equalsIgnoreCase("PreferredEmail"))
{
System.out.println("Please type in Preferred Email");
String ans = scan.next();
c.setEmailP(ans);
}
else if (select.equalsIgnoreCase("url"))
{
System.out.println("Please type in url");
String ans = scan.next();
c.setU(ans);
}
else if (select.equalsIgnoreCase("Comment"))
{
System.out.println("Please type in Comment");
String ans = scan.next();
c.setCom(ans);
}
else
{
System.out.println("I'm Sorry that is not a valid option.");
System.out.println("Please press Modify Button.");
}
System.out.println("All Contacts");
System.out.println();
for(int j = 0; j < list.size(); j++)
{
Contact z = list.get(j);
System.out.println("First Name "+z.getNameFL());
System.out.println("Last Name "+z.getNameLF());
System.out.println("Affiliation "+z.getAff());
System.out.println("Home Phone "+z.getHome());
System.out.println("Cell Phone "+z.getCell());
System.out.println("Preferred Phone "+z.getPref());
System.out.println("Home E-mail "+z.getEmailH());
System.out.println("Work E-mail "+z.getEmailW());
System.out.println("Preferred E-mail "+z.getEmailP());
System.out.println("url "+z.getU());
System.out.println("Comment "+z.getCom());
}
}
else if ("Delete".equals(e.getActionCommand()))
{
System.out.println("Which contact would you like to delete? (Pick number)");
for(int i = 0; i < list.size(); i++)
{
Contact x = list.get(i);
System.out.println(i+1+" "+x.getNameFL()+" "+x.getNameLF());
}
int choice = scan.nextInt();
list.remove(choice-1);
System.out.println("All Contacts");
System.out.println();
for(int j = 0; j < list.size(); j++)
{
Contact z = list.get(j);
System.out.println("First Name "+z.getNameFL());
System.out.println("Last Name "+z.getNameLF());
System.out.println("Affiliation "+z.getAff());
System.out.println("Home Phone "+z.getHome());
System.out.println("Cell Phone "+z.getCell());
System.out.println("Preferred Phone "+z.getPref());
System.out.println("Home E-mail "+z.getEmailH());
System.out.println("Work E-mail "+z.getEmailW());
System.out.println("Preferred E-mail "+z.getEmailP());
System.out.println("url "+z.getU());
System.out.println("Comment "+z.getCom());
}
}
else if ("DisplayAll".equals(e.getActionCommand()))
{
System.out.println("All Contacts");
System.out.println();
for(int i = 0; i < list.size(); i++)
{
Contact x = list.get(i);
System.out.println("First Name "+x.getNameFL());
System.out.println("Last Name "+x.getNameLF());
System.out.println("Affiliation "+x.getAff());
System.out.println("Home Phone "+x.getHome());
System.out.println("Cell Phone "+x.getCell());
System.out.println("Preferred Phone "+x.getPref());
System.out.println("Home E-mail "+x.getEmailH());
System.out.println("Work E-mail "+x.getEmailW());
System.out.println("Preferred E-mail "+x.getEmailP());
System.out.println("url "+x.getU());
System.out.println("Comment "+x.getCom());
}
}
else if ("DisplayI".equals(e.getActionCommand()))
{
System.out.println("Which contact would you like to display? (Pick number)");
for(int i = 0; i < list.size(); i++)
{
Contact x = list.get(i);
System.out.println(i+1+" "+x.getNameFL()+" "+x.getNameLF());
}
int choice = scan.nextInt();
System.out.println("First Name "+list.get(choice-1).getNameFL());
System.out.println("Last Name "+list.get(choice-1).getNameLF());
System.out.println("Affiliation "+list.get(choice-1).getAff());
System.out.println("Home Phone "+list.get(choice-1).getHome());
System.out.println("Cell Phone "+list.get(choice-1).getCell());
System.out.println("Preferred Phone "+list.get(choice-1).getPref());
System.out.println("Home E-mail "+list.get(choice-1).getEmailH());
System.out.println("Work E-mail "+list.get(choice-1).getEmailW());
System.out.println("Preferred E-mail "+list.get(choice-1).getEmailP());
System.out.println("url "+list.get(choice-1).getU());
System.out.println("Comment "+list.get(choice-1).getCom());
}
else if ("Save".equals(e.getActionCommand()))
{
System.out.println("NOT FINISHED! SAVE BUTTON");
// out = new PrintWriter("output.txt");
// for(int i = 0; i < list.size(); i++)
// {
// Contact x = list.get(i);
// out.println(x.getNameFL()+" "+x.getNameLF());
// }
// out.close();
}
else
{
JFileChooser chooser = new JFileChooser();
FileReader reader = null;
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File selectFile = chooser.getSelectedFile();
reader = new FileReader(selectFile);
}
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Contact List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ContactList newContentPane = new ContactList();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
static Contact inputContact(Scanner in)
{
Contact aContact = new Contact();
String resp;
System.out.println("Please type contact's First Name");
resp = in.next();
aContact.setNameFL(resp);
System.out.println("Please type contact's Last Name");
resp = in.next();
aContact.setNameLF(resp);
System.out.println("Please type contact's Affiliation");
resp = in.next();
aContact.setAff(resp);
System.out.println("Please type contact's Home Phone");
resp = in.next();
aContact.setHome(resp);
System.out.println("Please type contact's Cell Phone");
resp = in.next();
aContact.setCell(resp);
System.out.println("Please type contact's Preferred Phone");
resp = in.next();
aContact.setPref(resp);
System.out.println("Please type contact's Home E-mail");
resp = in.next();
aContact.setEmailH(resp);
System.out.println("Please type contact's Work E-mail");
resp = in.next();
aContact.setEmailW(resp);
System.out.println("Please type contact's Preferred E-mail");
resp = in.next();
aContact.setEmailP(resp);
System.out.println("Please type contact's url");
resp = in.next();
aContact.setU(resp);
System.out.println("Please type contact comments");
resp = in.next();
aContact.setCom(resp);
return aContact;
}
}
CODE
/**
Creating a Contact with several fields.
*/
public class Contact
{
/**
Sets the Contact with a first name
@param nameFL the Contact's first name
*/
public void setNameFL(String nameFL)
{
firstName = nameFL;
}
/**
Sets the Contact with a last name
@param aff the Contact's last name
*/
public void setNameLF(String nameLF)
{
lastName = nameLF;
}
/**
Sets the Contact with an affiliation
@param aff the Contact's affiliation
*/
public void setAff(String aff)
{
affiliation = aff;
}
/**
Sets the Contact with a home phone
@param home the Contact's home phone
*/
public void setHome(String home)
{
homePhone = home;
}
/**
Sets the Contact with a cell phone
@param cell the Contact's cell phone
*/
public void setCell(String cell)
{
cellPhone = cell;
}
/**
Sets the Contact with a preferred phone
@param pref the Contact's preferred phone
*/
public void setPref(String pref)
{
preferredPhone = pref;
}
/**
Sets the Contact with a home e-mail;
@param emailH the Contact's home e-mail
*/
public void setEmailH(String emailH)
{
homeEmail = emailH;
}
/**
Sets the Contact with a work e-mail
@param emailW the Contact's work e-mail
*/
public void setEmailW(String emailW)
{
workEmail = emailW;
}
/**
Sets the Contact with a preferred e-mail
@param emailP the Contact's preferred e-mail
*/
public void setEmailP(String emailP)
{
preferredEmail = emailP;
}
/**
Sets the Contact with an url
@param u the Contact's url
*/
public void setU(String u)
{
url = u;
}
/**
Sets the Contact with a comment
@param com the Contact's comment
*/
public void setCom(String com)
{
comment = com;
}
/**
Gets the contact's first name
@return the Contact's first name
*/
public String getNameFL()
{
return firstName;
}
/**
Gets the contact's last name
@return the Contact's last name
*/
public String getNameLF()
{
return lastName;
}
/**
Gets the contact's affiliation
@return the Contact's affiliation
*/
public String getAff()
{
return affiliation;
}
/**
Gets the contact's home phone
@return the Contact's home phone
*/
public String getHome()
{
return homePhone;
}
/**
Gets the contact's cell phone
@return the Contact's cell phone
*/
public String getCell()
{
return cellPhone;
}
/**
Gets the contact's preferred phone
@return the Contact's preferred phone
*/
public String getPref()
{
return preferredPhone;
}
/**
Gets the contact's home e-mail
@return the Contact's home e-mail
*/
public String getEmailH()
{
return homeEmail;
}
/**
Gets the contact's work e-mail
@return the Contact's work e-mail
*/
public String getEmailW()
{
return workEmail;
}
/**
Gets the contact's preferred e-mail
@return the Contact's preferred e-mail
*/
public String getEmailP()
{
return preferredEmail;
}
/**
Gets the contact's url
@return the Contact's url
*/
public String getU()
{
return url;
}
/**
Gets the contact comments
@return the Contact comments
*/
public String getCom()
{
return comment;
}
/**
Gets the contact comments
@return the Contact comments
*/
private String firstName;
private String lastName;
private String affiliation;
private String homePhone;
private String cellPhone;
private String preferredPhone;
private String homeEmail;
private String workEmail;
private String preferredEmail;
private String url;
private String comment;
}
*edit: Remember to use a forward slash in your closing code tags. Thanks for using them.
This post has been edited by Martyr2: 8 May, 2008 - 09:46 PM