?
Solved

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

Posted on 2009-02-15
14
Medium Priority
?
709 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.
0
Comment
Question by:gdkinney_2
  • 5
  • 5
  • 4
14 Comments
 
LVL 92

Expert Comment

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

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 23647222
>>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?
0
 

Author Comment

by:gdkinney_2
ID: 23647281
    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();
        }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:ksivananth
ID: 23647285
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();
        }
0
 
LVL 92

Expert Comment

by:objects
ID: 23647288
you don't close the session, and you do not appear to update value in Rockstar.

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 23647295
sorry, if it is AbstractTableModel, you can't do that! can you post you getValueAt method as well?
0
 

Author Comment

by:gdkinney_2
ID: 23647379
       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;
            }
        }
0
 

Author Comment

by:gdkinney_2
ID: 23647389
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;
            }
        }
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 2000 total points
ID: 23647402
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();
        }
0
 
LVL 92

Expert Comment

by:objects
ID: 23647409
you need to set the value in the setvalueat method. Something like

Rs.setxyz(...
0
 

Author Comment

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

Author Closing Comment

by:gdkinney_2
ID: 31547210
I do not really understand why it worked but it did.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 23647466
>>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!
0
 
LVL 92

Expert Comment

by:objects
ID: 23647472
>   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)


0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question