I deleted my other post because I was told it wasn't specific and to come back with a more specific question, here goes....
The program is supposed to take in contact info (First & Last name, phone, email) then you click an add button a Contact object will be created and go to an ArrayList. Then when a view button is clicked you will see all of the contacts that were added to it.
I have been watching tutorials and reading in books on how to use GUI and have been able to create a window with all the necessary stuff to add the contact info.
My issues: Where I'm struggling is how to then add this info to an array list
After I figure out how to add the info I will work on how to view it when the view button is clicked.
Any help or tips on moving me forward would be greatly appreciated!
public class InputForm extends JFrame {
private static JPanel panel;
private static JLabel firstNameLabel;
private static JLabel lastNameLabel;
private static JLabel phoneNumLabel;
private static JLabel emailLabel;
private static JLabel contactMessage;
private static JTextField firstNameInput;
private static JTextField lastNameInput;
private static JTextField phoneInput;
private static JTextField emailInput;
private static JButton addButton;
private static JButton viewButton;
private String fn;
private String ln;
private String ph;
private String em;
private List<String> list = new ArrayList<String>();
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);
if(event.getSource() == addButton) {
list.add(con);
}
else if(event.getSource() == viewButton) {
JOptionPane.showMessageDialog(null,"Contact Info:\n" + con);
}
else if(event.getSource() == viewButton) {
JOptionPane.showMessageDialog(null, con);
}
}
}
public static void main(String[] args) {
new InputForm();
}
}
Ok here is a contact class I created based on the advice from "Hovercraft Full of Eels".
public class Contact {
private String fn;
private String ln;
private String ph;
private String em;
private List<Contact> list = new ArrayList<Contact>();
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 fn;
}
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();
}
}