Results 1 to 4 of 4
  1. #1
    ayampanggang is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default Data from a model class won't show up in the table

    I have a simple model class that's derived from AbstractTableModel, which looks like this:
    Java Code:
    public class AddTrackModel extends AbstractTableModel {
    	public AddTrackModel() {
    		super();
    		content = new Object[10][9]; //10 rows, 9 columns
    		for (int i = 0; i < 10; ++i) {
    			content[i] = new Object[] { new Boolean(true), "","","","","","" }; // i'm adding data to the content array to be displayed on the table
    		}
    		this.fireTableDataChanged(); // fire a notification
    	}
    	public int getRowCount() {
    		return 0;
    	}
    	public int getColumnCount() {
    		return columns.length;
    	}
    	public Object getValueAt(int row, int col) {
    		return content[row][col];
    	}
    	public String getColumnName(int col) {
    		return columns[col];
    	}
    	private String[] columns = { "Check",
    			"Artist",
    			"Title", 
    			"CD No.", 
    			"Track No.", 
    			//"Album", 
    			//"Genre", 
    			//"BPM",
    			//"Description" 
    		};
    	private static final int CHECKBOX = 0, ARTIST = 1, TITLE = 2, CDNUM = 3, TRACKNUM = 4, ALBUM = 5, GENRE = 6, BPM = 7, DESC = 8;
    	private Object[][] content;
    }
    the problem:
    data that i entered into the content[][] array does not show up in the table.

    is there additional step i should do? all i know in order for the data to get displayed correctly is to implement the getValueAt which i did, in above code. And i even fired the fireTableDataChanged in the constructor right after i update the content[][] array, but the data still don't shows up in my table and i'm wondering why?

    do i need to implement an additional listener in my table that handles the drawing? i'm still using the original JTable.

    thank you very much! :)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public int getRowCount() {
        return content.length;
    }

  3. #3
    ayampanggang is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default

    that solved the problem. thank you!

  4. #4
    leniouf is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default Pretty much the same error

    Hi,

    I'm having a very similar problem but can't seem to figure out what exactly it is. I've got an Abstract Model Table that isn't showing anything in my JTable when I know that there is in fact a vector that is filled up with data. How do I make the JTable refresh so it displays the data. Did I also forget to add a certain function? Do I need a listener or a renderer? Here is my Abstract Model Table Class.

    import java.util.Vector;
    import javax.swing.table.AbstractTableModel;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;

    public class QueryTableModel extends AbstractTableModel{
    /**
    *
    */

    public static final int BOTH = 0;
    public static final int NONRECONCILED = 1;
    public static final int RECONCILED =2;

    Vector<String[]> cache;
    String[] headers;
    int colCount;


    public QueryTableModel(){
    super();
    cache = new Vector<String[]>();
    }
    public String getColumnName(int i){
    return headers[i];
    }
    public String[] getHeaders(){
    return headers;
    }
    public int getColumnCount(){
    return colCount;
    }
    public int getRowCount(){
    return cache.size();
    }
    public Object getValueAt (int row, int col){
    if(row < cache.size() && row >= 0 && col >= 0 && col < colCount){
    return ((String[]) cache.elementAt(row))[col];
    }
    return null;
    }

    public Object getRow(int row){
    return cache.elementAt(row);
    }

    public void setVector(ResultSet rs){
    if(cache.isEmpty() == false){
    cache.clear();
    }
    try{
    ResultSetMetaData meta = rs.getMetaData();
    colCount = meta.getColumnCount();

    headers = new String[colCount];
    for(int i=1; i<= colCount; i++){
    headers[i-1] = meta.getColumnName(i);
    }
    while(rs.next()){
    String[] record = new String[colCount];
    for(int j=0; j<colCount; j++){
    record[j] = rs.getString(j+1);
    }

    cache.addElement(record);
    }
    fireTableChanged(null);
    //fireTableStructureChanged();

    }catch (Exception e){
    e.printStackTrace();
    }

    }
    public void setVector(Vector<String[]> vect){
    if(cache.isEmpty() == false){
    cache.clear();
    }
    cache = (Vector<String[]>) vect.clone();
    this.fireTableChanged(null);
    //fireTableStructureChanged();
    }
    public void setHeaders(String[] head){
    headers = head;
    }
    public void setColumn(int col){
    colCount = col;
    }
    public Vector<String[]> getData(){
    return (Vector<String[]>) cache.clone();
    }
    public void clear(){
    cache.clear();
    }
    }

    Any ideas would be greatly appreciated.

Similar Threads

  1. Replies: 0
    Last Post: 02-09-2008, 09:22 PM
  2. Replies: 1
    Last Post: 02-01-2008, 02:21 PM
  3. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 07:20 AM
  4. Data formatting for the <display:table>
    By yuchuang in forum Web Frameworks
    Replies: 3
    Last Post: 12-14-2007, 11:52 AM
  5. Jsf, Filtering Data In A Table
    By Freddie in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-11-2007, 01:59 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
  •