1

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

editing

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

commit

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

change selection

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 );
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
nachokk
  • 14,363
  • 4
  • 24
  • 53

2 Answers2

2

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); 
}
Community
  • 1
  • 1
kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

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
    }
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • This notifies the table that all cells are changed. This is the wrong way to do this. Please post the code for your table and your table model classes and how they are being created – c.s. Jul 16 '13 at 13:09
  • 1
    wrong (you can to awaiting downvote by @kleopatra or @camickr) please to remove fireTableDataChanged(); this notifiers will recreating whole XxxTableModel, not == for better help sooner post an SSCCE, short, runnable, compilable, dot :-) – mKorbel Jul 16 '13 at 13:13
  • @c.s. now i change.. you are right but im not sure if i have to still calling `fireTableCellUpdated` – nachokk Jul 16 '13 at 13:13
  • @mKorbel `fireTableRowsUpdated` is bad? but im not sure if i have to still call `fireTableCellUpdated` – nachokk Jul 16 '13 at 13:14
  • not for why reason, why twice ??? ..., [to start with hardcoded coordinates, excelent code example by @Guillaume Polet](http://stackoverflow.com/a/14284027/714968), don't forget to override getColumnClass to avoiding inValue.toString(), – mKorbel Jul 16 '13 at 13:19
  • @c.s. maybe better ot convert "empty comments" to answer – mKorbel Jul 16 '13 at 13:21
  • @nachokk please when deleting text use strikethrough or a comment- out code. You have deleted `fireTableDataChanged();` line and now you have invalidated all our comments. A third person cannot follow the conversation. Leave the original line in a comment like: `//fireTableDataChanged(); - First approach. As pointed out this is wrong because it refreshes all table cells` – c.s. Jul 16 '13 at 13:22
  • @mKorbel i have override getColumnClass but i receive `Object`, you mean that i have to change value in `getValueAt` instead of doing calculation when `cantidad` property change? – nachokk Jul 16 '13 at 13:28
  • 1
    @nachokk "but i receive Object" please is there diff bettweens inValue.toString() and Integer value when column class is declared to Integer.Class, not there isn't both are Objects, AFAIK, sure as mentioned for better help sooner post an SSCCE, with hardcoded value for XxxTableModel, because, problably, maybe I'm sure that another N-bombs are in rest of important code :-) – mKorbel Jul 16 '13 at 13:33
  • @mKorbel mm i didn't code this but i setValue i inherit from a parent and parent doesn't have generics to change signature.. but now i solved my issue.. now is working with `fireTableRowsUpdated` xD – nachokk Jul 16 '13 at 13:42