473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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!


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
3 1737
madhoriya22
252 100+
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
r035198x
13,262 8TB
Create a Person class and store Person objects in one ArrayList.
Oct 4 '07 #3
JosAH
11,448 Expert 8TB
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

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

Similar topics

4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
6
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow =...
8
by: Bruno Alexandre | last post by:
Hi guys, I'm using a session to save an ArrayList, so I do not read Database everytime user reload the page or enter the site (the Data is consistent for all entire session time when the user is...
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
4
by: Energizer100 | last post by:
public class fly extends Actor { public fly() { setColor(Color.BLACK); } public fly(Color flyColor) { setColor(flyColor);
32
by: falconsx23 | last post by:
I am making a game called Set, it is a card game: here is a brief description of the rules: The game of Set is played with 81 cards, each of which has 4 properties (number, pattern, color and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.