Results 1 to 4 of 4

Thread: Add new contacts to an existing array list

  1. #1
    Join Date
    Oct 2009
    Posts
    82

    Add new contacts to an existing array list

    Hi
    I have done an program in java which stores information through array. The problem is that I can not add new contacts to the existing array through my method. the program has a GUI end and when a add button is pressed the information should be stored in the list of contact that is in the array. But I am not sure that how I will achieve this. In my program I have used two separate classes containing the get and the set methods. But the problem is that I have no idea how to implement these with my actionlisterner. Any one has an idea to do this. Please a sample code can help. Please post a sample code so that I can view it. Thanks in advance.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Add new contacts to an existing array list

    Hi
    I can explain you how can achieve this, but the implementation remains on your side. Where you see the ActionListener list of the button, add you class to it. And when the ActionEvent is triggered you can retrieve the text in the fields where you want. I am posting a code just go through it.
    Code:
    public class add extends JFrame implements ActionListener
    {
         public add()
         {
              // have the constructor as you have it, just add this line of code to the end
              addButton.addActionListener(this);
         }
         
         public void actionPerformed(ActionEvent e)
         {
              if (e.getActionCommand().equals("ADD"))
              {
                   // extract the info from the text fields you want
              }
         }
    }
    Any query do post back.

  3. #3
    Join Date
    Oct 2009
    Posts
    82

    Re: Add new contacts to an existing array list

    Hi
    Thanks for your code. As you said I have created the ActionListener, then I tried to get the text and store it in the array list that I had created. Bu the problem is that when i do all this I can not see any change. Here is my modified part of the code. Please help.
    Code:
    btnew= new JButton("Add Contact");
                    	panel2.add(btnew);
                    	btnew.addActionListener(new ActionListener() 
                    	
                    	{
                     	public void actionPerformed(ActionEvent event)
                     	{
                                if (event.getActionCommand().equals("ADD"))
    
                                records.add(new Record(txtName.getText(), txthomeTel.getText(), txtmobileTel.getText(), txtAddress.getText()));
             }
    Any comments on this.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Add new contacts to an existing array list

    Hello
    The getActionCommand() that you have used for the JButton class will return only the caption which is have on the button. I had just included ADD because I though that is what the button was for. Think you will need it to change to "Add contact" that would be a more proper name. Alternatively you can use the getSource() method and see if it works. Try and if any problem do post back.

Similar Threads

  1. Replies: 2
    Last Post: 01-02-2012, 07:08 PM
  2. How can you remove and insert an object in the array list ?
    By GOOL in forum Software Development
    Replies: 4
    Last Post: 03-01-2011, 11:29 PM
  3. Replies: 4
    Last Post: 17-11-2010, 10:50 PM
  4. Choose from a list of variables or array
    By AZUL in forum Software Development
    Replies: 4
    Last Post: 05-01-2010, 01:54 PM
  5. add a disk to an existing RAID 5 array on windows 2003 server
    By JimRBG in forum Windows Server Help
    Replies: 3
    Last Post: 08-10-2008, 09:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,411,707.30762 seconds with 17 queries