【问题标题】:Row refreshing when cell is edited编辑单元格时刷新行
【发布时间】:2013-07-16 21:41:40
【问题描述】:

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

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

但它们不显示,它们仅在我像这样更改选择时显示

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

我不确定当 fireTableCellUpdated 到 jtable 以重新绘制和重新验证时是否必须向 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 );
}

【问题讨论】:

  • 你能告诉我们这张桌子的代码吗?
  • mm 我不太确定我应该显示什么代码。我发布了表格模型,当我编辑单元格时会调用此方法

标签: java swing jtable abstracttablemodel


【解决方案1】:

如果更改特定单元格会更新同一行中的其他单元格(假设这是您所追求的),您的 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); 
}

【讨论】:

  • 我在测试时已经注意到+1,我忘了在这里编辑:),谢谢
  • 请与 fireTableRowsUpdated(inRow, inCol); 无关???如果他们以某种方式被束缚
  • 只是我的问题,有什么问题,还不清楚,避免任何猜测
【解决方案2】:

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

    @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
    }

【讨论】:

  • 这会通知表格所有单元格都已更改。这是错误的做法。请发布您的表格和表格模型类的代码以及它们是如何创建的
  • 错误(您可以等待@kleopatra 或@camickr 的否决)请删除 fireTableDataChanged();此通知程序将重新创建整个 XxxTableModel,而不是 == 以便更快地发布 SSCCE、简短、可运行、可编译、点 :-)
  • @c.s.现在我改变了..你是对的,但我不确定我是否还要打电话给fireTableCellUpdated
  • @mKorbel fireTableRowsUpdated 不好?但我不确定我是否还要打电话给fireTableCellUpdated
  • @nachokk“但我收到对象”当列类声明为 Integer.Class 时,inValue.toString() 和 Integer 值之间是否存在差异,不是两者都是对象,AFAIK,确保如前所述以获得更好的帮助,尽快发布 SSCCE,并为 XxxTableModel 提供硬编码值,因为很可能,我确信其他重要代码中还有另一个 N 炸弹 :-)
猜你喜欢
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多