So I have this code:
Code:
static ArrayList<String> firstname = new ArrayList();
static ArrayList<String> lastname = new ArrayList();
static ArrayList<String> address = new ArrayList();
static ArrayList<String> city = new ArrayList();
static ArrayList<String> state = new ArrayList();
static ArrayList<String> zipcode = new ArrayList();
public static void mainstuff(){
JTextField field1 = new JTextField(2),
field2 = new JTextField(2),
field3 = new JTextField(2);
JTextField field4 = new JTextField(2),
field5 = new JTextField(2),
field6 = new JTextField(2);
JPanel pane1 = new JPanel();
pane1.setLayout(new BoxLayout(pane1, BoxLayout.Y_AXIS));
pane1.add(new JLabel("First name: "));
pane1.add(field1);
pane1.add(Box.createHorizontalStrut(10));
pane1.add(new JLabel ("Last name: "));
pane1.add(field2);
pane1.add(Box.createHorizontalStrut(10));
pane1.add(new JLabel("Street Address: "));
pane1.add(field3);
pane1.add(Box.createHorizontalStrut(10));
pane1.add(new JLabel("City: "));
pane1.add(field4);
pane1.add(Box.createHorizontalStrut(10));
pane1.add(new JLabel("State: "));
pane1.add(field5);
pane1.add(Box.createHorizontalStrut(10));
pane1.add(new JLabel("ZIP code: "));
pane1.add(field6);
int personinput = JOptionPane.showConfirmDialog(null, pane1, "Please enter your:", JOptionPane.OK_CANCEL_OPTION);
System.out.println(personinput);
if (personinput == JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(null, "You must enter your information to vote.");
mainstuff();
}
for(int x = personinput; x==JOptionPane.OK_OPTION; x++)
{
firstname.add(field1.getText());
lastname.add(field2.getText());
address.add(field3.getText());
city.add(field4.getText());
state.add(field5.getText());
zipcode.add(field6.getText());
}
Right now, if I run just that, the input that the user puts in to the text fields is not added to the array. As in, if I do System.out.print(firstname);, nothing will appear.
How do you input the text entered to a JField to an ArrayList?
Probably an easy question, sorry, I'm new to java. thanks