Need Arraylist Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlos123
    New Member
    • Sep 2007
    • 53

    #1

    Need Arraylist Help

    Ok, im having EXTREME problems, with arraylists, i simply dont understand them, yes i have read your article. Would someone just take a look at this code, i know it sucks. Look at the areas that say "HELP!!!" next to them. But basicly i want to take data from textfields and put them into an arraylist. So the user inputs data, name, address, age all that, hits save (This is when it goes into the arraylist) the textfields clear, and they are ready to input another set of data into the array. Help me out guys, thanks alot!


    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    //import java.util.*;
    
    public class Main
    {
            JFrame f1;
            JPanel p1,p2;
           boolean clearall = false;
          
           
            int i = 0;
             List<FirstName> firsta = new ArrayList<FirstName>();  //problems!!!!11!1 HELP!!!!
           List<LastName> lasta = new ArrayList<LastName>();
           List<Age> agea = new ArrayList<Age>() ;
           List<City> citya = new ArrayList<City>();
           List<Street> streeta = new ArrayList<Street>();
          
           
           
           GP g1;
            String firststring,laststring,agestring,streetstring,citystring,test;
            JButton Bok,Bcc,clear,submit;
            JTextField first,last,age,street,city,gender;
            JLabel firstl,lastl,agel,streetl,cityl,genderl,monthl,statel,yearl;
        public Main()
        {  
           
            String[] year = { "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" , "2009", "2010"};
           
            firstl = new JLabel("First Name");
            lastl = new JLabel("Last Name");
            agel = new JLabel("Age");
            streetl = new JLabel("Street");
            cityl = new JLabel("City");
            genderl = new JLabel("Gender");
            monthl = new JLabel("Month");
            statel = new JLabel("State");
            yearl = new JLabel("Year");
            JComboBox yearcombo = new JComboBox(year);
            yearcombo.setSelectedIndex(29);
     
             yearcombo.setEditable(false);
            String[] month = { "January", "Febuary", "March", "April", "May", "June", "July", "October", "November", "December"};
            JComboBox monthcombo = new JComboBox(month);
            monthcombo.setEditable(false);
            monthcombo.setSelectedIndex(9);
           
            String[] state = { "Alabama AL", "Alaska AK" ,"Arizona AZ", "Arkansas AR", "California CA" ,"Colorado CO", "Connecticut CT" ,"Delaware DE", "Florida FL","Georgia GA", "Hawaii HI"," Idaho ID","Illinois IL" ,"Indiana IN", "Iowa IA", "Kansas KS" ,"Kentucky KY" ,"Louisiana LA", "Maine ME", "Maryland MD" ,"Massachusetts MA", "Michigan MI" ,"Minnesota MN" ,"Mississippi MS" ,"Missouri MO", "Montana MT" ,"Nebraska NE", "Nevada NV" ,"New Hampshire NH" ,"New Jersey NJ", "New Mexico NM", "New York NY", "North Carolina NC", "North Dakota ND" ,"Ohio OH" ,"Oklahoma OK", "Oregon OR" ,"Pennsylvania PA" ,"Rhode Island RI", "South Carolina SC", "South Dakota SD" ,"Tennessee TN", "Texas TX" ,"Utah UT", "Vermont VT" ,"Virginia VA" ,"Washington WA", "West Virginia WV", "Wisconsin WI" ,"Wyoming WY"};
            JComboBox statecombo = new JComboBox(state);
            statecombo.setSelectedIndex(49);
    
            String[] gender = {"M","F"};
            JComboBox gendercombo = new JComboBox(gender);
            gendercombo.setSelectedIndex(1);
         
            gendercombo.setEditable(false);
            
            MyActionListener actionListener = new MyActionListener();
        yearcombo.addActionListener(actionListener);
    
        gendercombo.addActionListener(actionListener);
     
        statecombo.addActionListener(actionListener);
            f1 = new JFrame("fafafffdas");
              f1.setSize(832,200);        
            Container c1 = f1.getContentPane();
            
            
            p1 = new JPanel();
            p1.setSize(1000,200);
            p1.setLayout(new BorderLayout());
            
            first = new JTextField(12);
            last = new JTextField(12);
            age = new JTextField(12);
            street = new JTextField(12);
            city = new JTextField(12);
            clear = new JButton("Clear");
            clear.addActionListener(new ButtonListener());
            submit = new JButton("Submit");
            submit.addActionListener(new ButtonListener());
            Bok = new JButton("Save");
             Bok.addActionListener(new ButtonListener());
            Bcc = new JButton("Quit");
             Bcc.addActionListener(new ButtonListener());
             
             
             
              // Get number of items
           //  int numyear = yearcombo.getItemCount();
          // int numgender = statecombo.getItemCount();
          // int numstate = gendercombo.getItemCount();
           
        
             
            p2 = new JPanel();  //panel to add JLabel and TextField
            
            g1 = new GP();    
             p1.add(g1,BorderLayout.CENTER);
           
            g1.add(firstl);  
            g1.add(first);
            g1.add(lastl);
            g1.add(last);
            g1.add(agel);
            g1.add(age);
            g1.add(yearl);
            g1.add(yearcombo);
            g1.add(genderl);
            g1.add(gendercombo);
            g1.add(streetl);
            g1.add(street);
            g1.add(cityl);
            g1.add(city);
            g1.add(statel);
            g1.add(statecombo);
            p2.add(submit);
            p2.add(Bok);
            p2.add(clear);
            p2.add(Bcc);
    
            p1.add(p2,BorderLayout.SOUTH);      //adds p2 to south (Bottom) Graphics Panel will be added to Center    
            c1.add(p1);
           
            f1.show();
    
        }
     
           
    
        
      
       
      
    
       
         public class MyActionListener implements ActionListener
         {
            public void actionPerformed(ActionEvent evt)
            {
                JComboBox yearcombo = (JComboBox)evt.getSource();
        
                // Get the new item
                Object yearselect = yearcombo.getSelectedItem();
                
            
            
                JComboBox gendercombo = (JComboBox)evt.getSource();
        
                // Get the new item
                Object genderselect = gendercombo.getSelectedItem();
                
            
            
               JComboBox statecombo = (JComboBox)evt.getSource();
        
                // Get the new item
                Object stateselect = statecombo.getSelectedItem();
                
            }
        }
        
        private class ButtonListener implements ActionListener
        {
          public void actionPerformed (ActionEvent event)
          {
              
              
            if (event.getSource() == clear)
            {
                clearall = true;
                if (clearall = true)
                {
                street.setText(null);
                first.setText(null);
                last.setText(null);
                age.setText(null);
                city.setText(null);
                gender.setText(null);
                
                }
            }    
          
            if (event.getSource() == Bok)
               {
            
                firststring = first.getText(); //HELP!!!!!!!! 
                FirstName f = new FirstName (firststring);
                firsta.add(i,firststring);
                laststring = last.getText();
                lasta.add(i,laststring);
                agestring = age.getText();
                agea.add(i,agestring);
                streetstring = street.getText();
                streeta.add(i,streetstring);
                citystring = city.getText();
                citya.add(i,citystring);
                i++;
                System.out.println(i);
                System.out.println(firsta[i]);
            }
            clearall = true;
                if (clearall = true)
                {
                street.setText(null);
                first.setText(null);
                last.setText(null);
                age.setText(null);
                city.setText(null);
                gender.setText(null);
                
                }
               
               
            if (event.getSource() == Bcc)
               {
    
                   f1.dispose();
               }
            if (event.getSource() == submit)
            {
               
                firststring = first.getText();
                laststring = last.getText();
                agestring = age.getText();
                streetstring = street.getText();
                citystring = city.getText();
             clearall = true;
    
            }
    
              g1.repaint();  
            }
        }
        
        
    }
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    Originally posted by carlos123
    Ok, im having EXTREME problems, with arraylists, i simply dont understand them, yes i have read your article. Would someone just take a look at this code, i know it sucks. Look at the areas that say "HELP!!!" next to them. But basicly i want to take data from textfields and put them into an arraylist. So the user inputs data, name, address, age all that, hits save (This is when it goes into the arraylist) the textfields clear, and they are ready to input another set of data into the array. Help me out guys, thanks alot!
    Hi,
    What errors you are getting? Please specify them.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Create a Person class and store Person objects in one ArrayList.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by carlos123
        Ok, im having EXTREME problems, with arraylists, i simply dont understand them, yes i have read your article.
        Ok, read it again (copied vebatim from your previous thread):

        Originally posted by JosAH
        IMHO there should be a 'Person' class with 'firstName', 'lastName', 'age' etc. members
        and all the getXXX methods; setXXX methods are probably not necessary for all
        members. I'd implement an 'equals()' and 'hashCode()' method for that class as
        well so that it can safely be stored in a Set<Person> set that mimics a database.
        Probably a 'toString()' method comes in handy as well.

        Last but not least: I'd avoid arrays like the plague for purposes like these.
        kind regards,

        Jos

        Comment

        Working...