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.
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.
>>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?
setValueAt(Object value, int row, int col) method.
>>
seems like the model used is not updated properly! can u post the code?
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.getSessionFa ctory().op enSession( );
String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
Query q = session.createSQLQuery(tem p);
q.executeUpdate();
fireTableDataChanged();
}
public void setValueAt(Object value, int row, int column) {
Rockstar rs = rockstars.get(row);
Session session = HibernateUtil.getSessionFa
String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
Query q = session.createSQLQuery(tem
q.executeUpdate();
fireTableDataChanged();
}
try this,
public void setValueAt(Object value, int row, int column) {
Rockstar rs = rockstars.get(row);
Session session = HibernateUtil.getSessionFa ctory().op enSession( );
String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
Query q = session.createSQLQuery(tem p);
q.executeUpdate();
super.setValueAt( value, row, column ) ;
fireTableDataChanged();
}
public void setValueAt(Object value, int row, int column) {
Rockstar rs = rockstars.get(row);
Session session = HibernateUtil.getSessionFa
String temp = "update Rockstars set " + getColumnNm(column) + " = " + getValue(value, column) + " where ID = " + rs.getId();
Query q = session.createSQLQuery(tem
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?
ASKER
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;
}
}
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;
}
}
ASKER
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;
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to set the value in the setvalueat method. Something like
Rs.setxyz(...
Rs.setxyz(...
ASKER
ksivananth: Your last post worked when I did an rs.set... I don't fully understand how that worked though could someone explain please?
ASKER
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!
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)
as I mentioned earier you needded to update your value in your list (as well as in your db)
whats the getValueAt() method lookk like?