JTable doesn't retrieve the correct data

Hi there,

I'm having a problem with my JTable and its driving me nuts. Please help!

In short, what I'm trying to do:

I have a set of messages coming into NewSetOfSubDialog.notify() below. Each message is essentially a long string. I want to parse this string and dispaly that data in tabular form. The column names of the table are within the message, so I have to create the columns at runtime. The code is below and I've tried to comment it.

The problem I'm having is with the getValueAt(int rowIndex,int colIndex)

method in my data model. The problem is that the colIndex does not change -- it always has a value of 0. Because of this, when my data is accessed (RowData.getDataForColumn(colIndex)) only the values for column 0 are retrieved.

Now I know my getColumnCount() method returns 0, but according to this tutorial (http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node169.html) getColumnCount() should return zero when the JTable.setAutoCreateColumnsFromModel property is set to false.

When I dispaly the table, all cells have the same value. All the cells are filled in, but all with the same value.

What am I doing wrong here? How to I get getColumnCount() to pass in different values for the column index? If I set getColumnCount() to a different value, I get new columns in addition to the columns I'm manually creating.

Here is the code where the table is created.

publicclass NewSetOfSubDialogextends MonitorDialog{

...

private MessagesTableModel2 messagesTableModel;

private JTable messagesTable;

...

public NewSetOfSubDialog(MonitorFrame owner, String brokerID, CilentMonitorCommandManager comm){

...

}

publicvoid buildContentPanel(){

...

messagesTableModel =new MessagesTableModel2();

messagesTable =new JTable(messagesTableModel);

...

}

publicvoid notify(Object o){

//adding my own columns to the table at runtime

messagesTable.setAutoCreateColumnsFromModel(false);

//get messages

Map setMsgs = (Map) o;

Iterator it;

//determine columns

messagesTableModel.setColumns(o);

//create columns

Vector<TableColumn> allColumns = messagesTableModel.getColumns();

for (it = allColumns.iterator(); it.hasNext();){

TableColumn col = (TableColumn) it.next();

col.setPreferredWidth(100);

messagesTable.addColumn(col);

}

//parse messages in data model

for (it = setMsgs.values().iterator(); it.hasNext();){

SubscriptionMessage sMsg = (SubscriptionMessage) it.next();

SubscriptionMessageParser smp =new SubscriptionMessageParser(sMsg);

filterOptionsHeaderSet.addAll(smp.getMessageHeaderFilterOptions());

filterOptionsContentSet.addAll(smp.getMessageContentFilterOptions());

messagesTableModel.addMessageToModel(sMsg);

}

messagesTableModel.fireTableDataChanged();

...

}

}

Here is the code of my custom data model.

package monitor.dialogs.messagedialogs;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.TableColumn;

import message.AdvertisementMessage;

import message.Message;

import message.Subscription;

import message.SubscriptionMessage;

publicclass MessagesTableModel2extends AbstractTableModel{

public Map<String, Integer>headerColumnsIndices;

public Map<String, Integer>contentColumnsIndices;

public Vector<TableColumn>headerColumns;

public Vector<TableColumn>contentColumns;

public Vector<RowData>allData;

public MessagesTableModel2(){

headerColumnsIndices =new HashMap<String, Integer>();

contentColumnsIndices =new HashMap<String, Integer>();

headerColumns =new Vector<TableColumn>();

contentColumns =new Vector<TableColumn>();

allData =new Vector<RowData>();

}

publicint getColumnCount(){

// since I set setAutoCreateColumnsFromModel to false, I read

// that this should be 0

return 0;

}

publicint getRowCount(){

// TODO Auto-generated method stub

return allData.size();

}

public Object getValueAt(int arg0,int arg1){

//arg0 = rowIndex

//arg1 = columnIndex

//get row

RowData row = allData.get(arg0);

//find column value

Object value = row.getDataForColumn(arg1);

//int arg0 changes

//int arg1 is always 0

System.out.println("row: " + arg0 +" col: " + arg1);

return value;

}

//inner class to handle row data

privateclass RowData{

private Message msg;

private Object[] dataPerColumn;

public RowData(Message msg){

this.msg = msg;

initArray();

setMsg();

}

privatevoid initArray(){

dataPerColumn =new Object[headerColumns.size() + contentColumns.size() + 1];

for (int i=0; i<dataPerColumn.length; i++){

dataPerColumn[i] ="1";

}

}

publicvoid setMsg(){

Map headerData = msg.getAllHeaderFields();

Iterator it;

//set header data

for (it=headerData.keySet().iterator(); it.hasNext();){

String fieldName = it.next().toString();

//get column index (number)

Integer colIndex = headerColumnsIndices.get(fieldName);

//place data in appropriate location

System.out.println("FieldName: " + fieldName +" Data: " + headerData.get(fieldName));

dataPerColumn[colIndex.intValue()] = headerData.get(fieldName);

System.out.println("dataPerColumn: " + dataPerColumn[colIndex.intValue()]);

}

//set content data

}

public Object getDataForColumn(int columnIndex){

if ((columnIndex >= 0) && (columnIndex < dataPerColumn.length)){

return dataPerColumn[columnIndex];

}elsereturn"";

}

}

publicvoid addMessageToModel(Message msg){

//insert the data into the model

RowData row =new RowData(msg);

allData.add(row);

fireTableStructureChanged();

fireTableDataChanged();

}

public Vector<TableColumn> getColumns(){

Vector<TableColumn> allColumns =new Vector<TableColumn>();

allColumns.addAll(headerColumns);

allColumns.addAll(contentColumns);

return allColumns;

}

publicvoid setColumns(Object setOfMessages){

Map messagesMap = (Map) setOfMessages;

Iterator it;

for (it = messagesMap.values().iterator(); it.hasNext();){

Message msg = (Message) it.next();

addAnyNewHeaderColumns(msg);

addAnyNewContentColumns(msg);

}

}

privatevoid addAnyNewHeaderColumns(Message msg){

Map headerFields = msg.getAllHeaderFields();

Iterator it;

for(it = headerFields.keySet().iterator(); it.hasNext();){

String fieldName = it.next().toString();

if (headerColumnsIndices.containsKey(fieldName)){

//do nothing

}else{

headerColumnsIndices.put(fieldName,new Integer(headerColumnsIndices.size()+1));

TableColumn col =new TableColumn();

col.setHeaderValue(fieldName);

headerColumns.add(col);

}

}

}

privatevoid addAnyNewContentColumns(Message msg){

Iterator it;

SubscriptionMessage subMsg = (SubscriptionMessage) msg;

Subscription sub = subMsg.getSubscription();

Map predicateMap = sub.getPredicateMap();

for (it = predicateMap.keySet().iterator(); it.hasNext();){

String fieldName = it.next().toString();

if (contentColumnsIndices.containsKey(fieldName)){

//do nothing

}else{

contentColumnsIndices.put(fieldName,new Integer(contentColumnsIndices.size()+1));

TableColumn col =new TableColumn();

col.setHeaderValue(fieldName);

contentColumns.add(col);

}

}

}

}

Message was edited by:

themiddle

[13868 byte] By [themiddlea] at [2007-11-26 12:58:04]
# 1
> TableColumn col = new TableColumn();The TableColumn is used to tell the table which column from the TableModel contains the data for this particular column in the view of the table.You didn't specify a column number so it defaults to column 0.
camickra at 2007-7-7 16:55:05 > top of Java-index,Desktop,Core GUI APIs...
# 2
That was the problem...thanks!
themiddlea at 2007-7-7 16:55:06 > top of Java-index,Desktop,Core GUI APIs...