Link to home
Start Free TrialLog in
Avatar of gdkinney_2
gdkinney_2

asked on

When I edit a JTable cell in Java and persist it using Hibernate and then click off the cell my new value is saved but cell changes back to original value

When I edit a JTable cell in Java and persist it using Hibernate and then click off the cell my new value is saved but the cell changes back to original value when the cell loses focus.  I am using a TableModel for my JTable that extends AbstractTableModel.  I implement the method
public void setValueAt(Object value, int row, int col)
in my class that extends AbstractTableModel.  In this method I use an update to write the new value to the database through Hibernate.  The new value does get written to the database but when I click off the table cell the cell I changed goes back to the old value.  So the JTable does not refresh itself.  I do call the method fireTableDataChanged(); at the end of the
setValueAt(Object value, int row, int col) method.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you need to also change the value in your table model. are you sure u are doing that?
whats the getValueAt() method lookk like?

>>The new value does get written to the database but when I click off the table cell the cell I changed goes back to the old value.  So the JTable does not refresh itself.  I do call the method fireTableDataChanged(); at the end of the
setValueAt(Object value, int row, int col) method.
>>

seems like the model used is not updated properly! can u post the code?
Avatar of gdkinney_2
gdkinney_2

ASKER

    Here is the method in my TableModel class that extends AbstractTableModel:  

     public void setValueAt(Object value, int row, int column) {
            Rockstar rs = rockstars.get(row);
            Session session = HibernateUtil.getSessionFactory().openSession();
            String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
            Query q = session.createSQLQuery(temp);
            q.executeUpdate();
            fireTableDataChanged();
        }
try this,

public void setValueAt(Object value, int row, int column) {
            Rockstar rs = rockstars.get(row);
            Session session = HibernateUtil.getSessionFactory().openSession();
            String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
            Query q = session.createSQLQuery(temp);
            q.executeUpdate();
            super.setValueAt( value, row, column ) ;
            fireTableDataChanged();
        }
you don't close the session, and you do not appear to update value in Rockstar.

sorry, if it is AbstractTableModel, you can't do that! can you post you getValueAt method as well?
       public Object getValueAt(int row, int column) {
            Rockstar ps = rockstars.get(row);
            switch (column) {

                case 0:
                    return ps.getName();
                case 1:
                    return ps.getAge();
                case 3:
                    return ps.getId();
                default:
                    return null;
            }
        }
Slight typo in previous post

public Object getValueAt(int row, int column) {
            Rockstar rs = rockstars.get(row);
            switch (column) {

                case 0:
                    return rs.getName();
                case 1:
                    return rs.getAge();
                case 3:
                    return rs.getId();
                default:
                    return null;
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you need to set the value in the setvalueat method. Something like

Rs.setxyz(...
ksivananth: Your last post worked when I did an rs.set...  I don't fully understand how that worked though could someone explain please?
I do not really understand why it worked but it did.
>>I don't fully understand how that worked though could someone explain please?

though you have code for saving the changes to DB, you don't have code for updating the values used for show in table. actually the table uses the getValueAt method to update the view( display ). so when you set the value to Rockstar which is used in getValueAt, the updated value is shown!
>   I don't fully understand how that worked though could someone explain please?

as I mentioned earier you needded to update your value in your list (as well as in your db)