3

I am developing a costum JTable for a client of mine.

I had just finished the column model when I started on the table model. Most of the functions that are related to columns in the table model are actually aliases for the functions in the column model.

Anyway, something really strange happened. I hope someone can help me with it:

  1. The JTable shows the column correctly. Which means getColumnCount and getColumnName is working properly.

  2. The number of rows is shown correctly. Which means getRowCount is working properly.

  3. The number of cells for each row is shown properly, since the «getColumnCount» in the table model returns the value of the getColumnCount at the column model.

Now, here comes the weird thing:

The value for the first cell of each row is correct. But it remains the same for all of the other cells in the same row.

I assumed, like most of you already did, that getValueAt has something wrong in it. So, I decided to hardcode a call to it after the table is rendered. And guess what: the value came back correct.

After some debugging I found out that its the JTable that is calling 'getValueAt(rowIndex, 0)', instead of 'getValueAt(rowIndex, columnIndex)'.

Can anyone help me with this? Best regards...

THE TABLE MODEL

/**
 * Returns the value to be displayed for this column at this row index.
 * @param rowIndex
 * @param columnIndex
 * @return
 */
public Object getValueAt(int rowIndex, int columnIndex) {
    System.out.println(String.format("LOS_TableModel: getValueAt(%d, %d)", rowIndex, columnIndex));
    LOS_TableCell cell = this.getCell(columnIndex, rowIndex);
    if(cell == null) return null;
    else return cell.value;
}

/**
 * Returns the LOS_TableCell at the specified JTable indexes
 * @param index
 * @return
 */
public LOS_TableCell getCell(int columnIndex, int rowIndex) {
    for(Object o_row : this.rows) {
        if(o_row.getClass() == LOS_TableRowGroup.class) {
            LOS_TableRowGroup row = (LOS_TableRowGroup) o_row;
            LOS_TableCell cell = row.getCell(columnIndex, rowIndex);
            if(cell != null) return cell;
        }

        else {
            LOS_TableRow row = (LOS_TableRow) o_row;
            for(LOS_TableCell cell : row.cells) 
                if(cell.column.tableIndex == columnIndex && cell.row.tableIndex == rowIndex) return cell;
        }
    }
    return null;
}

/**
 * Returns the number of visible columns
 * @return
 */
public int getColumnCount() {
    int result = this.columnModel.getColumnCount();
    System.out.println("LOS_TableModel : getColumnCount() : " + result);
    return result;
}

/**
 * Returns the total of displayed rows
 * @return
 */
public int getRowCount() {
    int rowCount = 0;
    for(LOS_TableRow row : this.rows) {
        if(row.visible) rowCount += 1;
        if(row.getClass() == LOS_TableRowGroup.class)
            rowCount += ((LOS_TableRowGroup) row).getDisplayedRowCount();
    }
    return rowCount;
}

THE COLUMN MODEL

/**
 * Returns an enumeration of columns.
 * @return
 */
public Enumeration<TableColumn> getColumns() {
    Vector<TableColumn> columns = new Vector<TableColumn>();
    for(LOS_TableColumn column : this.columns) 
        if(column.visible) columns.add((TableColumn)column);
    return columns.elements();
}

/**
 * Used by the JTable to get a column index.
 * @param columnIdentifier
 * @return
 */
public int getColumnIndex(Object columnIdentifier) {
    System.out.println("LOS_ColumnModel: getColumnIndex(" + columnIdentifier + ")");
    for(LOS_TableColumn column : this.columns)
        if(column.getIdentifier().equals(columnIdentifier)) return column.tableIndex;
    return -1;
}

/**
 * Return a column using its JTable index
 * @param columnIndex
 * @return
 */
public TableColumn getColumn(int columnIndex) {
    System.out.println("LOS_ColumnModel : getColumn(" + columnIndex + ")");
    for(LOS_TableColumn column : this.columns)
        if(column.tableIndex == columnIndex) return column;
    throw new IndexOutOfBoundsException("" + columnIndex);
}

And that hardcoded test. 2 Rows with 3 cells each:

System.out.println("=========> " + model.getValueAt(1, 2)); // Outputs 'Cell 1,2'

2 Answers 2

5

The value for the first cell of each row is correct. But it remains the same for all of the other cells in the same row.

Your TableColumn is being created incorrectly.

The TableColumn contains the index into the TableModel for which column contains the data to display. When you create a TableColumn this value defaults to 0 (so the same data is displayed on all columns).

I don't see anywhere in your code where you actually create the TableColumn but you should NOT be using:

TableColumn tc = new TableColumn();

Instead you should be using:

TableColumn tc = new TableColumn( modelIndex );
1
  • @flash, glad you appreciated the answer, even if the origianl poster didn't :)
    – camickr
    Aug 26, 2011 at 14:26
0

The table column index isn't specified because the index is changed dynamicly, as the columns can be hidden or set back to visible when needed.

This was actually being done by the table model.

Anyway, I figured it out. I don't really know what happened. I dropped the table model, because I figured I really didn't need it and did the table model from scratch. It's working fine now.

Thanks anyway =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.