3

我正在为我的一个客户开发一个服装 JTable。

当我开始研究表格模型时,我刚刚完成了列模型。大多数与表模型中的列相关的函数实际上是列模型中函数的别名。

无论如何,发生了一件非常奇怪的事情。我希望有人可以帮助我:

  1. JTable 正确显示该列。这意味着 getColumnCount 和 getColumnName 工作正常。

  2. 行数显示正确。这意味着 getRowCount 工作正常。

  3. 每行的单元格数显示正确,因为表模型中的 «getColumnCount» 返回列模型中 getColumnCount 的值。

现在,奇怪的事情来了:

每行的第一个单元格的值是正确的。但对于同一行中的所有其他单元格,它保持不变。

我假设,就像你们大多数人已经做过的那样,getValueAt 有问题。因此,我决定在表格呈现后对它的调用进行硬编码。猜猜看:这个值是正确的。

经过一些调试,我发现它是调用'getValueAt(rowIndex,0)'而不是'getValueAt(rowIndex,columnIndex)'的JTable。

谁能帮我这个?此致...

表模型

/**
 * 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;
}

柱模型

/**
 * 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);
}

还有那个硬编码的测试。2 行,每行 3 个单元格:

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

2 回答 2

5

每行的第一个单元格的值是正确的。但对于同一行中的所有其他单元格,它保持不变。

您的 TableColumn 创建不正确。

TableColumn 包含 TableModel 的索引,该列包含要显示的数据。创建 TableColumn 时,此值默认为 0(因此所有列上都显示相同的数据)。

我在您的代码中没有看到您实际创建 TableColumn 但您不应该使用的任何地方:

TableColumn tc = new TableColumn();

相反,您应该使用:

TableColumn tc = new TableColumn( modelIndex );
于 2011-01-30T16:30:07.193 回答
0

未指定表列索引,因为索引是动态更改的,因为列可以在需要时隐藏或设置回可见。

这实际上是由表模型完成的。

无论如何,我想通了。我真的不知道发生了什么。我放弃了表格模型,因为我认为我真的不需要它并从头开始制作表格模型。现在工作正常。

无论如何谢谢=)

于 2011-02-01T14:43:47.717 回答