Results 1 to 8 of 8
  1. #1
    stefan_banu is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default how to make a phonebook, stored the data in a collection and displaying in a jtable?

    hi guys,

    i'm trying to make a phonebook, but i don't know why my input data is not stored in ArrayList and not displayed in the jlist

    private ArrayList<Person> pers = new ArrayList<Person>();

    public void add(Person p){

    pers.add(p);
    System.out.println(p);
    }


    but nothing appear on the jlist form.
    for this method add i must use firecontentschanged?

    if someone can help me?

  2. #2
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: how to make a phonebook, stored the data in a collection and displaying in a jtab

    I would recommend the use of a HashMap instead of ArrayList
    As for JTable I'll leave it to the moderators coz Im not familiar with it

  3. #3
    stefan_banu is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default Re: how to make a phonebook, stored the data in a collection and displaying in a jtab

    Quote Originally Posted by Kagiso View Post
    I would recommend the use of a HashMap instead of ArrayList
    As for JTable I'll leave it to the moderators coz Im not familiar with it

    thank you for your quick answer, anyone else have other ideas?

  4. #4
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: how to make a phonebook, stored the data in a collection and displaying in a jtab

    Post your code...and we'll see if I can help

    Your Person class should be something like this

    public class Person {
    private String name;
    private long number;

    public Person(String nam, long nm) {
    nam = name;
    num = number;
    }
    }
    make sure you don't add null references to your ArrayList
    Last edited by Kagiso; 05-19-2013 at 04:22 PM.

  5. #5
    stefan_banu is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default Re: how to make a phonebook, stored the data in a collection and displaying in a jtab

    this are the classes:
    this is the class for Person
    Java Code:
    public class Abonat implements Serializable {
    
        private String nume;
        private String prenume;
        private int CNP;
        private int NrTel;
    
        public Abonat(String nume, String prenume, int CNP, int NrTel) {
            this.nume = nume;
            this.prenume = prenume;
            this.CNP = CNP;
        }
    
        public String getNume() {
            return nume;
        }
    
        public String getPrenume() {
            return prenume;
        }
    
        public int getCNP() {
            return CNP;
        }
    
        @Override
        public String toString() {
            return "Abonatul cu nume " + nume + " prenume " + prenume + "si CNP: " + CNP;
        }
    }
    this is the class for the "model" that holds the methods for adding, for deleting, for editing a person
    Java Code:
    public class PhoneBook extends AbstractListModel{
    
     private ArrayList<Abonat> persons = new ArrayList<Abonat>();
    
    
          public void adding(Abonat abonat){
    
         persons.add(abonat); // dont know how to implement this method
    
        }
          void delete(int pozition) {
    
          persons.remove(p); // dont know how to implement this method
    
        }
          public void editing(){
             // no idea
    
          }
    }
    i don't know if this implementation is correct, in the GUI i want to input the name, the phone number etc. and displayed in a jtable?or jlist?
    should i use somthing else then ArrayList?

    the phonebook must save all the inputs on the hard disk

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    236
    Rep Power
    1

    Default Re: how to make a phonebook, stored the data in a collection and displaying in a jtab

    Look at this How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) on how to create o table model

    after you create your own table model, when you create the JTable you call setModel(TableModel m)

  7. #7
    stefan_banu is offline Member
    Join Date
    May 2013
    Posts
    7
    Rep Power
    0

    Default Re: how to make a phonebook, stored the data in a collection and displaying in a jtab

    thank you so much , i'll go trough right now, the problem is that i dont know how to connect to the gui, i hope i will find the answer in this tutorials,

    hope to finish before my dead line

    thanks

  8. #8
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: how to make a phonebook, stored the data in a collection and displaying in a jtab

    First you'll have to use your ArrayList properly
    to add and remove things on our list, all we need is to call add() and remove()... there's no need to implement our own version of those methods

    this is How you use them, if I had
    ArrayList<String> names = new ArrayList<String>();

    adding things to this list would be done like this
    names.add("Kagiso");

    In your case
    person.add(new Abonat(nume,prenume,CNP, NrTel)) // this is the proper way

    go check the api-Docs java.util.ArrayList

Similar Threads

  1. Replies: 3
    Last Post: 09-15-2012, 04:29 PM
  2. Replies: 0
    Last Post: 08-04-2011, 05:16 PM
  3. How to make element unique in Java Collection
    By jeanjiang in forum New To Java
    Replies: 2
    Last Post: 04-25-2011, 11:53 AM
  4. Value stored in variable not displaying.
    By mainy in forum New To Java
    Replies: 5
    Last Post: 03-10-2009, 04:52 PM
  5. Replies: 2
    Last Post: 02-02-2009, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •