我在使用这个JTable时遇到了问题。我像这样编辑一个单元格

然后我按enter键提交更改。在这里,我希望表gui用新的值刷新。

但它们不会显示,只有当我像这样更改选择时才会显示

当我编辑一个单元格时,fireTableCellUpdated( inRow, inCol );是在tableModel中的方法调用。
我不确定在对jtable执行fireTableCellUpdated操作时是否必须将listener添加到tableModel,以便重新绘制和重新验证。
一些代码:
这在tableModel中被调用。
@Override
public void setValueAt( Object inValue, int inRow, int inCol ) {
ProductRow productRow = (ProductRow)( getRowsData().get(inRow) );
//more code
productRow.setCantidad( inValue.toString() ); // when this is called all properties are updated from ProductRow
fireTableCellUpdated( inRow, inCol );
}发布于 2013-07-16 21:03:46
我最终解决了这个问题,但我不太确定这是否是最好的解决方法。
@Override
public void setValueAt( Object inValue, int inRow, int inCol ) {
ProductRow productRow = (ProductRow)( getRowsData().get(inRow) );
//more code
productRow.setCantidad( inValue.toString() ); // when this is called all properties of productRow are changed.
//fireTableCellUpdated( inRow, inCol );// this don't refresh cause i change the row also
//fireTableDataChanged(); - First approach. As pointed out this is wrong because it refreshes all table cells
fireTableRowsUpdated(inRow,inRow); // adding this
}发布于 2013-07-16 21:38:41
如果更改特定单元格会更新同一行中的其他单元格(假设这就是您要更改的单元格),则last attempt in your answer将使用正确的方法,只是参数不正确:-)
@Override
public void setValueAt( Object inValue, int inRow, int inCol ) {
ProductRow productRow = (ProductRow)( getRowsData().get(inRow) );
// when this is called all properties of productRow are changed.
productRow.setCantidad( inValue.toString() );
// note: both parameters are _row_ coordinates
fireTableRowsUpdated(inRow, inRow);
}https://stackoverflow.com/questions/17676792
复制相似问题