Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,245 Java Programmers. There are 2,264 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Creating Open & Save As applications

 
Reply to this topicStart new topic

Creating Open & Save As applications

veryconfused
post 7 May, 2008 - 04:12 PM
Post #1


New D.I.C Head

*
Joined: 28 Apr, 2008
Posts: 7



Okay what I'm trying to do is create a Contactlist class that can create a JFileChooser Dialog box to open any file and one to save to any. I am pretty much clueless on where to start. If someone could give me an idea on how I create the Dialog boxes could you help me please. Thank you.

CODE
import java.util.Scanner;
import java.util.ArrayList;
import java.io.PrintWriter;

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;
    PrintWriter out;

    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(x.getNameFL()+" "+x.getNameLF());
              }
            }
         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, 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(z.getNameFL()+" "+z.getNameLF());
              }
           }
         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);
           }
         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(x.getNameFL()+" "+x.getNameLF());
              }
            }
         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(list.get(choice-1).getNameFL()+" "+list.get(choice-1).getNameLF());    
           }

         // Not Working!
         else if ("Save".equals(e.getActionCommand()))
         {
          
          System.out.println(" SAVE BUTTON");
          //  out = new PrintWriter("output.txt");
          //  out.println("All Contacts");
          //  out.println();
          //  for(int i = 0; i < list.size(); i++)
          //    {
          //        Contact x = list.get(i);
          //        out.println(x.getNameFL()+" "+x.getNameLF());
          //    }
          //  out.close();
          }
         else
          // Not Working
            System.out.println(" OPEN BUTTON");
             {
             //JFileChooser chooser = new JFileChooser();
             // FileReader in = null;
              }
    }


    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;

}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


Martyr2
post 8 May, 2008 - 10:03 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 3,561

I answered your question in the other thread. So be sure to read that one and you will see that you are not far off with how you have it setup. Enjoy! smile.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 08:37AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month