Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Aug 2012
    Posts
    145
    Thanks
    0
    Thanked 26 Times in 26 Posts
    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? Jesus is returning very soon

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •