tags:

views:

70

answers:

1

I'm making an address book and I need to cycle through my contacts. The contacts are imported from file and are read into the JTextFields as so:

name
phone
mobile
address

How do I go about doing this?

I've tried, it runs but the buttons do nothing.

edit: now using this:

           public void importContacts() 
           {
     try 
            {

             BufferedReader infoReader = new BufferedReader(new FileReader("../files/example.txt"));
            txtName   .setText(readLine(infoReader));
        txtPhone  .setText(readLine(infoReader));
        txtMobile .setText(readLine(infoReader));
        txtAddress.setText(readLine(infoReader));

            } 
            catch (IOException ioe) 
            {

            JOptionPane.showMessageDialog(null, ioe.getMessage());
        }

}

+1  A: 

In your importContacts() method

you do :

 txtName.setText(Name.get(0));
 txtPhone.setText(Phone.get(0));
 txtMobile.setText(Mobile.get(0));
 txtAddress.setText(Address.get(0));

instead of .get(0) I think it should be .get(index) according to your code

--Edit--

or to avoid re-importing your contact here what your previous() method should be :

public void Previous()
            {
                    if (index > 0)
                    {
                            index--;
                    }
                    txtName.setText(Name.get(index));
                    txtPhone.setText(Phone.get(index));
                    txtMobile.setText(Mobile.get(index));
                    txtAddress.setText(Address.get(index));

            }

  public void Next()
            {
                    if(index < temp.size() - 1){
                 index++;
               }
                    txtName.setText(Name.get(index));
                    txtPhone.setText(Phone.get(index));
                    txtMobile.setText(Mobile.get(index));
                    txtAddress.setText(Address.get(index));

            }

-Final edit, code source availabe at pastebin

Nettogrof
i just used 0 to prove how to access the first element of the list
Anthony Forloney
You can do in your previous() and next method() txtName.setText(Name.get(index)); (and all others)
Nettogrof
so get(1) will show the next contact?? i'm really stuck on this one!
addiosamigo
in the above example, yes. To show the next contact you must keep track of the index of the ArrayList you are at. Increasing the index value will give you the next contact, decrease the index value will give you the previous contact
Anthony Forloney
check my edit to help you
Nettogrof
please note, that it will only show the next contact because the example shows the get method retrieving the 0th element of the ArrayList
Anthony Forloney
nettogrof, i've put that in but its not working?
addiosamigo
Did you assign a actionLister to your button ? Because in your last code in pasteBin the line was in comment " // " and if it still doesn't work, try to add this.repaint(); at the end of your previous and next method
Nettogrof
I have made the change, and it's work look at http://pastebin.com/m4b81c9e5
Nettogrof
hi nettogrof sorry for wasting your time. i've now changed the way i import the file, its a lot easier this way. can I still use your code?
addiosamigo
nettogrof, i've noticed that your way shows the contacts as soon as it runs, do i need an import button and can i get rid of my method importContacts??
addiosamigo
The new way that you import your file, it'll read only the first contact, not all of them. With the code I provide , yes you can remove the Import button, but the ImportContacts() is use to read the file.
Nettogrof
can i amend your code so that it works with my new code? thanks
addiosamigo
new code here http://pastebin.com/d12aa3ab3
addiosamigo