By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

Need Arraylist Help

P: 53
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!


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. //import java.util.*;
  5.  
  6. public class Main
  7. {
  8.         JFrame f1;
  9.         JPanel p1,p2;
  10.        boolean clearall = false;
  11.  
  12.  
  13.         int i = 0;
  14.          List<FirstName> firsta = new ArrayList<FirstName>();  //problems!!!!11!1 HELP!!!!
  15.        List<LastName> lasta = new ArrayList<LastName>();
  16.        List<Age> agea = new ArrayList<Age>() ;
  17.        List<City> citya = new ArrayList<City>();
  18.        List<Street> streeta = new ArrayList<Street>();
  19.  
  20.  
  21.  
  22.        GP g1;
  23.         String firststring,laststring,agestring,streetstring,citystring,test;
  24.         JButton Bok,Bcc,clear,submit;
  25.         JTextField first,last,age,street,city,gender;
  26.         JLabel firstl,lastl,agel,streetl,cityl,genderl,monthl,statel,yearl;
  27.     public Main()
  28.     {  
  29.  
  30.         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"};
  31.  
  32.         firstl = new JLabel("First Name");
  33.         lastl = new JLabel("Last Name");
  34.         agel = new JLabel("Age");
  35.         streetl = new JLabel("Street");
  36.         cityl = new JLabel("City");
  37.         genderl = new JLabel("Gender");
  38.         monthl = new JLabel("Month");
  39.         statel = new JLabel("State");
  40.         yearl = new JLabel("Year");
  41.         JComboBox yearcombo = new JComboBox(year);
  42.         yearcombo.setSelectedIndex(29);
  43.  
  44.          yearcombo.setEditable(false);
  45.         String[] month = { "January", "Febuary", "March", "April", "May", "June", "July", "October", "November", "December"};
  46.         JComboBox monthcombo = new JComboBox(month);
  47.         monthcombo.setEditable(false);
  48.         monthcombo.setSelectedIndex(9);
  49.  
  50.         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"};
  51.         JComboBox statecombo = new JComboBox(state);
  52.         statecombo.setSelectedIndex(49);
  53.  
  54.         String[] gender = {"M","F"};
  55.         JComboBox gendercombo = new JComboBox(gender);
  56.         gendercombo.setSelectedIndex(1);
  57.  
  58.         gendercombo.setEditable(false);
  59.  
  60.         MyActionListener actionListener = new MyActionListener();
  61.     yearcombo.addActionListener(actionListener);
  62.  
  63.     gendercombo.addActionListener(actionListener);
  64.  
  65.     statecombo.addActionListener(actionListener);
  66.         f1 = new JFrame("fafafffdas");
  67.           f1.setSize(832,200);        
  68.         Container c1 = f1.getContentPane();
  69.  
  70.  
  71.         p1 = new JPanel();
  72.         p1.setSize(1000,200);
  73.         p1.setLayout(new BorderLayout());
  74.  
  75.         first = new JTextField(12);
  76.         last = new JTextField(12);
  77.         age = new JTextField(12);
  78.         street = new JTextField(12);
  79.         city = new JTextField(12);
  80.         clear = new JButton("Clear");
  81.         clear.addActionListener(new ButtonListener());
  82.         submit = new JButton("Submit");
  83.         submit.addActionListener(new ButtonListener());
  84.         Bok = new JButton("Save");
  85.          Bok.addActionListener(new ButtonListener());
  86.         Bcc = new JButton("Quit");
  87.          Bcc.addActionListener(new ButtonListener());
  88.  
  89.  
  90.  
  91.           // Get number of items
  92.        //  int numyear = yearcombo.getItemCount();
  93.       // int numgender = statecombo.getItemCount();
  94.       // int numstate = gendercombo.getItemCount();
  95.  
  96.  
  97.  
  98.         p2 = new JPanel();  //panel to add JLabel and TextField
  99.  
  100.         g1 = new GP();    
  101.          p1.add(g1,BorderLayout.CENTER);
  102.  
  103.         g1.add(firstl);  
  104.         g1.add(first);
  105.         g1.add(lastl);
  106.         g1.add(last);
  107.         g1.add(agel);
  108.         g1.add(age);
  109.         g1.add(yearl);
  110.         g1.add(yearcombo);
  111.         g1.add(genderl);
  112.         g1.add(gendercombo);
  113.         g1.add(streetl);
  114.         g1.add(street);
  115.         g1.add(cityl);
  116.         g1.add(city);
  117.         g1.add(statel);
  118.         g1.add(statecombo);
  119.         p2.add(submit);
  120.         p2.add(Bok);
  121.         p2.add(clear);
  122.         p2.add(Bcc);
  123.  
  124.         p1.add(p2,BorderLayout.SOUTH);      //adds p2 to south (Bottom) Graphics Panel will be added to Center    
  125.         c1.add(p1);
  126.  
  127.         f1.show();
  128.  
  129.     }
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.      public class MyActionListener implements ActionListener
  140.      {
  141.         public void actionPerformed(ActionEvent evt)
  142.         {
  143.             JComboBox yearcombo = (JComboBox)evt.getSource();
  144.  
  145.             // Get the new item
  146.             Object yearselect = yearcombo.getSelectedItem();
  147.  
  148.  
  149.  
  150.             JComboBox gendercombo = (JComboBox)evt.getSource();
  151.  
  152.             // Get the new item
  153.             Object genderselect = gendercombo.getSelectedItem();
  154.  
  155.  
  156.  
  157.            JComboBox statecombo = (JComboBox)evt.getSource();
  158.  
  159.             // Get the new item
  160.             Object stateselect = statecombo.getSelectedItem();
  161.  
  162.         }
  163.     }
  164.  
  165.     private class ButtonListener implements ActionListener
  166.     {
  167.       public void actionPerformed (ActionEvent event)
  168.       {
  169.  
  170.  
  171.         if (event.getSource() == clear)
  172.         {
  173.             clearall = true;
  174.             if (clearall = true)
  175.             {
  176.             street.setText(null);
  177.             first.setText(null);
  178.             last.setText(null);
  179.             age.setText(null);
  180.             city.setText(null);
  181.             gender.setText(null);
  182.  
  183.             }
  184.         }    
  185.  
  186.         if (event.getSource() == Bok)
  187.            {
  188.  
  189.             firststring = first.getText(); //HELP!!!!!!!! 
  190.             FirstName f = new FirstName (firststring);
  191.             firsta.add(i,firststring);
  192.             laststring = last.getText();
  193.             lasta.add(i,laststring);
  194.             agestring = age.getText();
  195.             agea.add(i,agestring);
  196.             streetstring = street.getText();
  197.             streeta.add(i,streetstring);
  198.             citystring = city.getText();
  199.             citya.add(i,citystring);
  200.             i++;
  201.             System.out.println(i);
  202.             System.out.println(firsta[i]);
  203.         }
  204.         clearall = true;
  205.             if (clearall = true)
  206.             {
  207.             street.setText(null);
  208.             first.setText(null);
  209.             last.setText(null);
  210.             age.setText(null);
  211.             city.setText(null);
  212.             gender.setText(null);
  213.  
  214.             }
  215.  
  216.  
  217.         if (event.getSource() == Bcc)
  218.            {
  219.  
  220.                f1.dispose();
  221.            }
  222.         if (event.getSource() == submit)
  223.         {
  224.  
  225.             firststring = first.getText();
  226.             laststring = last.getText();
  227.             agestring = age.getText();
  228.             streetstring = street.getText();
  229.             citystring = city.getText();
  230.          clearall = true;
  231.  
  232.         }
  233.  
  234.           g1.repaint();  
  235.         }
  236.     }
  237.  
  238.  
  239. }
  240.  
  241.  
Oct 4 '07 #1
Share this Question
Share on Google+
3 Replies


madhoriya22
100+
P: 252
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.
Oct 4 '07 #2

10K+
P: 13,264
Create a Person class and store Person objects in one ArrayList.
Oct 4 '07 #3

Expert 10K+
P: 11,448
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):

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
Oct 4 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.