Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-16-2012, 04:11 AM   PM User | #1
ezra
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ezra is an unknown quantity at this point
inputting in to jfields

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
ezra is offline   Reply With Quote
Old 11-16-2012, 07:16 AM   PM User | #2
Brandnew
New Coder

 
Join Date: Aug 2012
Posts: 50
Thanks: 0
Thanked 11 Times in 11 Posts
Brandnew is an unknown quantity at this point
Hi ezra,

i'm somewhat learning java as well (haven't really fiddled with it for some time til now). i was able to get the printout for your coding (System.out.println) but the issue that pops up deals with the number 0 showing on top of the console print out..hope this helps

Code:
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CodingForums extends JFrame{
	
	  static ArrayList<String> firstname = new ArrayList<String>();
	    static ArrayList<String> lastname = new ArrayList<String>();
	    static ArrayList<String> address = new ArrayList<String>();
	    static ArrayList<String> city = new ArrayList<String>();
	    static ArrayList<String> state = new ArrayList<String>();
	    static ArrayList<String> zipcode = new ArrayList<String>();
	

public static void main(String [] args){
        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.");
           main(args);
        }
        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());
            mainstuff();
        }

        
}


private static void mainstuff() {
	// TODO Auto-generated method stub
System.out.println(firstname);
System.out.println(lastname);
System.out.println(zipcode);
}
}
__________________
1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for?
Brandnew is offline   Reply With Quote
Old 11-16-2012, 02:47 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That will add to the list. The loop doesn't make any sense though; personInput will only be either OK_OPTION or CANCEL_OPTION, so looping it is a horrendously bad idea. All it takes is changing that equality to a different comparison (equality should be avoided in loop conditions in favour of >= or <= (either with or without equals) instead) and then you have an infinite loop.

Its a bad way of doing it, you shouldn't be using a confirm dialog for this, and at the very least not without a frame so you have something to return control to. Dialogs are really more designed for "Are you sure you want to do this?" type actions, inputs into form fields really make more sense on a frame with buttons that have events added on them.

I'm not sure what's up with all these arraylists though, these look to me that you should have a single ArrayList<Person> and create a Person class to deal with all those properties. Otherwise you have many disjointed lists that cannot be properly sorted and retain any meaningful relationship.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:41 PM.


Advertisement
Log in to turn off these ads.