We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

gdkinney_2
gdkinney_2 asked
on
Medium Priority
770 Views
Last Modified: 2013-12-29
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.
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

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

ksivananthVice President

Commented:
>>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?

Author

Commented:
    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();
        }
ksivananthVice President

Commented:
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();
        }
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you don't close the session, and you do not appear to update value in Rockstar.

ksivananthVice President

Commented:
sorry, if it is AbstractTableModel, you can't do that! can you post you getValueAt method as well?

Author

Commented:
       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;
            }
        }

Author

Commented:
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;
            }
        }
Vice President
Commented:
hoping that rockstars is a list, 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();
            if( column == 0 ) rs.setName( value ) ;
            else if( column == 1 ) rs.setAge( ( Integer )value ) ;
            fireTableDataChanged();
        }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you need to set the value in the setvalueat method. Something like

Rs.setxyz(...

Author

Commented:
ksivananth: Your last post worked when I did an rs.set...  I don't fully understand how that worked though could someone explain please?

Author

Commented:
I do not really understand why it worked but it did.
ksivananthVice President

Commented:
>>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!
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
>   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)


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.