小编Joã*_*pes的帖子

JTable,TableModel和TableColumnModel - 发生了奇怪的事情

我正在为我的客户开发一个服务器JTable.

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

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

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

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

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

现在,出现了奇怪的事情:

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

我假设,就像你们大多数人已经做过的那样,getValueAt中有一些错误.因此,我决定在呈现表格后对其进行硬编码.并猜测:价值回归正确.

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

谁能帮我这个?最好的祝福...

表模型

/**
 * 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 …
Run Code Online (Sandbox Code Playgroud)

java swing jtable tablemodel

3
推荐指数
1
解决办法
4359
查看次数

标签 统计

java ×1

jtable ×1

swing ×1

tablemodel ×1