Row refreshing when cell is edited

时间秒杀一切 提交于 2019-12-02 11:14:30

问题


Im having problems with this JTable. I edit a cell like this

Then i commit changes pressing enter. Here im hoping that table gui refresh with new values.

But they aren't show, they are only show when i change selection like this

fireTableCellUpdated( inRow, inCol ); is the method call when in the tableModel when i edit a cell.

Im not sure if i have to add listener to the tableModel when fireTableCellUpdated to the jtable to repaint and revalidate.

Some Code :

This is called in the 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 );
}

回答1:


If changing a particular cell updates other cells in the same row (assuming that's what you are after), your last attempt in your answer uses the correct method, just with incorrect parameter :-)

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


I solved it adding this at last, but im not quite sure if it's the best way to solve.

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


来源:https://stackoverflow.com/questions/17676792/row-refreshing-when-cell-is-edited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!