Need help with getting TreeMap information into a JTable...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Need help with getting TreeMap information into a JTable...

  1. #1
    Join Date
    Jan 2009
    Posts
    6

    Unhappy Need help with getting TreeMap information into a JTable...

    Hi guys,

    I have this mini telephone book assignment that I have to do, but now I'm stuck at this one part and I can't get it figured out.

    Just a bit of background on what I have so far in my program:

    1. A 'Name and Surname' TextField for the user to insert a persons name and surname.
    2. A 'Number' TextField for the user to insert a persons phone number.
    3. Add Button - To add a new person and his telephone number.
    4. Remove Button - To remove a person along with the telephone number.
    5. Retrieve Button - To get the appropriate number of a specific person.
    6. View All Entries Button - To display a list of all the names and numbers added so far in a JTable.
    7. Clear Textfields - To clear both of the textfields.

    I used a TreeMap<String, String> to store the information of the names and numbers input by the user.

    I also used Regular Expressions to check if the information are correct which the user has input in the name and number textfields.

    * A user may only add a new contact or retrieve a number if the information is in the correct format.
    * A user may only remove an entry when the number has been retrieved first.

    Now here is my problem that I'm having trouble with:

    At point number six (above) - I do not know how to get the information from the TreeMap to get it to display in the JTable,
    I have created a method called myTable() which the actionPerformed method for the "View All Entries Button" will call to display the JTable
    with all the entries in it.

    I'm not sure where exactly the problem is in my code for the 'myTable()' but when I input about five contacts along with their numbers
    and click on the "View All Entries Button" no entry is displayed? Why is this?

    Code for myTable:

    Code:
    public void myTable() {
        	JFrame frame = new JFrame();
        	frame.setTitle("Phonebook Entries");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
            int a = tm.size();
            String columnNames[] = {"Name and Surname", "Phone Number"};
            Object rows[][] = new Object[a][2];
            
            JTable t = new JTable(rows, columnNames);
            JScrollPane scrollPane = new JScrollPane(t);
            
            frame.setContentPane(scrollPane);
            
            for(Map.Entry entry : tm.entrySet()) {
                entry.getKey();   
                entry.getValue();  
            }
     
            
            t = new JTable(rows, columnNames);
            scrollPane.getViewport().add(t);
            
            frame.setVisible(true);
            frame.pack();
        }
    Please Help me out with this.

    Thanks in advance

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Have you reviewed the Java Tutorial's section on Using Tables at http://java.sun.com/docs/books/tutor...ts/table.html? Take a look at creating a TableModel, too, because you can directly populate your table from the Map you have created. [As the tutorial says, the Table is not a data structure, but a view of the contents of your data structure]

Similar Threads

  1. Dynamic Rows in JTable
    By ClanDaith in forum Java
    Replies: 0
    Last Post: 12-09-2008, 03:33 PM
  2. Replies: 0
    Last Post: 10-14-2008, 02:44 PM
  3. JTable Cell Renderers
    By freesoft_2000 in forum Java
    Replies: 3
    Last Post: 11-25-2006, 04:53 AM
  4. Remove exchange from a site
    By andriano in forum Enterprise
    Replies: 1
    Last Post: 10-02-2000, 03:54 PM
  5. Replies: 1
    Last Post: 05-19-2000, 12:59 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center