编辑单元格时的行刷新

时间:2013-07-16 12:40:54

标签: java swing jtable abstracttablemodel

我遇到了这个JTable的问题。我编辑这样的单元格

editing

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

commit

但是他们没有表演,他们只是在我改变这样的选择时才显示

change selection

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 );
}

2 个答案:

答案 0 :(得分:2)

如果更改特定单元格会更新同一行中的其他单元格(假设您正在使用的那些单元格),则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 :(得分:1)

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

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