JAVA中row与column,java-JTable,TableModel和TableColumnModel-奇怪的事情

我正在为我的客户开发Costum JTable.

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

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

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

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

>由于表模型中的《 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 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 getColumns() {

Vector columns = new Vector();

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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值