1

I have a new question that I would love some guidance on.

I am creating a GUI program that takes in Contact info from the user and every time the addButton is clicked a Contact object is created and added to an array list, then when the viewButton is clicked that info is all displayed.

I finally got the JTextField info into an ArrayList but, I'm not sure how to add multiple contact info. I added firstNameInput.setText(""); after the first contact info is entered to clear the screen but that also clears the ArrayList. I'm thinking I need some sort of loop but the one I created ended up creating a endless loop. This is all in the ActionPerformed method... I understand why its an endless loop but this is the first time I've used an ArrayList so I'm getting confused.

Pointers in the right direction is greatly appreciated.

public class InputForm extends JFrame {
    private JPanel panel;
    private JLabel firstNameLabel;
    private JLabel lastNameLabel;
    private JLabel phoneNumLabel;
    private JLabel emailLabel;
    private JTextField  firstNameInput;
    private JTextField  lastNameInput;
    private JTextField  phoneInput;
    private JTextField  emailInput;
    private JButton addButton;
    private JButton viewButton;
    private String fn;
    private String ln;
    private String ph;
    private String em;
    private List<Contact> list = new ArrayList<Contact>();

public InputForm() {
    int windowWidth = 650;
    int windowHeight = 550;

    //Window title
    setTitle("CONTACT FORM");
    //Set window size.
    setSize(windowWidth, windowHeight);
    //Exit on close 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Buld the panel and add it the the JFrame.
    buildPanel();
    //Add the panel to the frames content pane
    add(panel);
    //Display the window.
    setVisible(true);   
}

private void buildPanel() {

    panel = new JPanel();
    addButton = new JButton("Add Contact");
    viewButton = new JButton("View Contacts");

    firstNameLabel = new JLabel("First Name");
    firstNameInput = new JTextField(10);
    lastNameLabel = new JLabel("Last Name: ");
    lastNameInput = new JTextField(10);
    phoneNumLabel = new JLabel("Phone Number: ");
    phoneInput = new JTextField(10);
    emailLabel = new JLabel("Email: ");
    emailInput = new JTextField(10);

    /*Add labels, text fields and buttons to the panel*/
    panel.add(firstNameLabel);
    panel.add(firstNameInput);
    panel.add(lastNameLabel);
    panel.add(lastNameInput);
    panel.add(phoneNumLabel);
    panel.add(phoneInput);
    panel.add(emailLabel);
    panel.add(emailInput);
    panel.add(addButton);
    panel.add(viewButton);

    theHandler handler = new theHandler();
    addButton.addActionListener(handler);
    viewButton.addActionListener(handler);
}


private class theHandler implements ActionListener {

   public void actionPerformed(ActionEvent event) {
        fn = firstNameInput.getText();
        ln = lastNameInput.getText();
        ph = phoneInput.getText();
        em = emailInput.getText();
        Contact con = new Contact(fn, ln, ph, em);

    while(event.getSource() == addButton) {
        list.add(con);
        fn = firstNameInput.getText();
        ln = lastNameInput.getText();
        ph = phoneInput.getText();
        em = emailInput.getText();
        list.add(con);  
        firstNameInput.setText("");
        lastNameInput.setText("");
        phoneInput.setText("");
        emailInput.setText("");
    }           
    if(event.getSource() == viewButton) {
        JOptionPane.showMessageDialog(null,"Contact Info:\n" + con);        
    }
}   
}
}

CONTACT CLASS

public class Contact {
    private String fn;
    private String ln;
    private String ph;
    private String em;

public Contact(String fn, String ln, String ph, String em) { 
    this.fn = fn;
    this.ln = ln;
    this.ph = ph;
    this.em = em;
}
    public String getFirstName() {
        return fn;
    }
    public void setFirstName(String fn) {
        this.fn = fn;
    }   
    public String getLastName() {
        return ln;
    }
    public void setLastName(String ln) {
        this.ln = ln;
    }
    public String getPhone() {
        return ph;
    }
    public void setPhone(String ph) {
        this.ph = ph;
    }
    public String getEmail() {
        return em;
    }   
    public void setEmail(String em) {
        this.em = em;
    }

    public String toString() {
        return "First Name: " + getFirstName() + "\n" +
                "Last Name: " + getLastName() + "\n" +
                "Phone Number: " + getPhone() + "\n" +
                "Email: " + getEmail() + "\n";
    }

public static void main(String[] args) {
    new InputForm();
}
}

1 Answer 1

6

Everytime you want to add a Contact to your list you need to create a new Contact. You do that with new Contact. Also, you don't need a loop. Every time the event fires you add one to the List. Something like,

// Contact con = new Contact(fn, ln, ph, em);
// while(event.getSource() == addButton) {
if (event.getSource() == addButton) {
    fn = firstNameInput.getText();
    ln = lastNameInput.getText();
    ph = phoneInput.getText();
    em = emailInput.getText();
    Contact con = new Contact(fn, ln, ph, em);
    list.add(con); // <-- adds the Contact to the list.  
    firstNameInput.setText("");
    lastNameInput.setText("");
    phoneInput.setText("");
    emailInput.setText("");
}
Sign up to request clarification or add additional context in comments.

6 Comments

Oh ok, I actually had that first but it wasn't working for me so it must be how I'm "viewing" the contacts after they've been added? It seems like it's only printing the last contact info entered into the list. Any tis on how to print out all the info on a JOptionPane?
Nevermind, it looks like I was printing the object and not the actual list. I think that should fix it. Thanks for all your help!!!
Try calling list.toString(). You might also prefer to build a large formatted String, but that's really a different question (and you should provide a MCVE).
Why list.toString() and not just list? Also, what do you mean by a large formatted String? I apologize for not constructing the question correctly I always try to make it as specific as possible. Is there too much code? I figured you guys would want to see all of it to figure out what was going on...
Also, when the info is printed on the JOption window there is a [ , ].... In the beginning a [ then a comma in between and then another ]. Is there anything to do to get rid of that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.