首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编辑单元格时刷新行

编辑单元格时刷新行
EN

Stack Overflow用户
提问于 2013-07-16 20:40:54
回答 2查看 2K关注 0票数 1

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

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

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

当我编辑一个单元格时,fireTableCellUpdated( inRow, inCol );是在tableModel中的方法调用。

我不确定在对jtable执行fireTableCellUpdated操作时是否必须将listener添加到tableModel,以便重新绘制和重新验证。

一些代码:

这在tableModel中被调用。

代码语言:javascript
复制
@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 );
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-16 21:03:46

我最终解决了这个问题,但我不太确定这是否是最好的解决方法。

代码语言:javascript
复制
    @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
    }
票数 1
EN

Stack Overflow用户

发布于 2013-07-16 21:38:41

如果更改特定单元格会更新同一行中的其他单元格(假设这就是您要更改的单元格),则last attempt in your answer将使用正确的方法,只是参数不正确:-)

代码语言:javascript
复制
@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); 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17676792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档