Results 1 to 1 of 1
  1. #1
    darrenw is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Smile creating a AbstractTableModel

    I intend to have initially a empty Jtable that is then updated according to the values in a hash map defined like this: searchList = new HashMap<String, Set<String>>();

    In the interface I have two combo lists that have values in them that the user can then change and then click on update, once the button is clicked it adds the values to the Map.

    I would then like to have the two column table populated with the added details.

    I've been working through the example here How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) but I'm having difficulty getting the data in to the table, also the table does not seem to like having null values if the user has not added anything yet.

    The class that handles the data can produce the Object[][] and colLabels,

    any pointers would be grateful?

    here is the class:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package lcdoffline;

    import java.util.*;

    /**
    *
    * @author Darren
    */
    public class Helper1618
    {

    private Map<String, Set<String>> searchList;

    public Helper1618()
    {
    searchList = new HashMap<String, Set<String>>();
    }

    //takes two values and adds them to a hashset
    public void addCourseType(String aIndex, String aSubject)
    {
    Set a = new HashSet<String>();
    if (searchList.containsKey(aIndex))
    {
    searchList.get(aIndex).add(aSubject);
    }
    else
    {
    a.add(aSubject);
    searchList.put(aIndex, a);
    }
    }

    public void removeCourseType(String aIndex, String aSubject)
    {
    if (searchList.containsKey(aIndex))
    {
    if (searchList.get(aIndex).contains(aSubject))
    {
    searchList.get(aIndex).remove(aSubject);
    }
    }
    }

    public void removeQualType(String aIndex)
    {
    if (searchList.containsKey(aIndex))
    {
    searchList.remove(aIndex);
    }
    }

    public int getRows()
    {
    int r = 0;
    for (String type : searchList.keySet())
    {

    for (String subj : searchList.get(type))
    {
    r++;
    }
    }
    return r;
    }

    //prints out values
    public void getValues()
    {

    for (String type : searchList.keySet())
    {
    System.out.println(type + ":");
    for (String subj : searchList.get(type))
    {
    System.out.println(subj + ",");
    }
    }
    }

    private Object[][] getColData()
    {

    Object[][] o = null;
    int col = 0;
    // int row = 0;
    for (String type : searchList.keySet())
    {
    for (String subj : searchList.get(type))
    {
    o[col][0] = type;
    o[col][1] = subj;
    }
    col++;
    }
    return o;
    }
    }
    Last edited by darrenw; 10-31-2010 at 12:14 PM. Reason: added code

Similar Threads

  1. The right way to inherit AbstractTableModel
    By skywriter in forum New To Java
    Replies: 3
    Last Post: 10-19-2010, 11:28 AM
  2. DefaultTableModel vs AbstractTableModel
    By chyrl in forum AWT / Swing
    Replies: 4
    Last Post: 05-02-2010, 12:10 PM
  3. Replies: 3
    Last Post: 11-07-2009, 05:25 PM
  4. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-25-2009, 11:45 PM
  5. Replies: 4
    Last Post: 04-01-2009, 02:03 AM

Posting Permissions

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