Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronizing JTable with a non Observable<List> in Netbeans  RSS feed

 
Ste Hutton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've been using netbeans to build a database application and it all seems to be working perfectly apart from 1 issue I'm experiencing.

I have a JTable which is bound to a List<> of an entity class inside an entity class. The JTable itself isn't editable, but when you select a row from the list, I pass the object inside the List to a JDialog, where each field is bound to the entity objects attributes. The changes when being made are immediately reflected in the JTable. However if I cancel the database transaction, the database rollsback the transaction. However the JTable remains updated and out of synchronization with the database.

Does anyone know any way around this?

Here is the code I am using to save / refresh the database

 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do have so many calls to 'entityManager.getTransaction().begin()' ?
and you re calling begin even after a 'rollback' or 'commit' is done...

as for the rollback issues, I think you can do a entityManager.refresh for that entity which should then have the correct data which will be reflected in the JTable...
 
Ste Hutton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranganathan Kaliyur Mannar wrote:Why do have so many calls to 'entityManager.getTransaction().begin()' ?
and you re calling begin even after a 'rollback' or 'commit' is done...

as for the rollback issues, I think you can do a entityManager.refresh for that entity which should then have the correct data which will be reflected in the JTable...


Many thanks for the reply.

I had placed begin where I thought they were needed (not made a database app for some time so a little rusty).

I've altered the code a little but it still isn't updating the JTable to reflect the changes.



the entityManager.refresh(entity) in the for loop is what you were referring to refresh the entity right?

Many thanks in advance
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, where is the call to refresh()? It should be called whenever an exception occurs and rollback has happened.

More importantly, you need not refresh the entire contents. Just refresh the entity that you are referring to (i.e the one you tried to save and rolled back). And normally, whenever the table data model is changed, it would call its fireXXX methods to notify the JTable to update itself. You need to see if that happens in this case.
 
Ste Hutton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranganathan Kaliyur Mannar wrote:First of all, where is the call to refresh()? It should be called whenever an exception occurs and rollback has happened.

More importantly, you need not refresh the entire contents. Just refresh the entity that you are referring to (i.e the one you tried to save and rolled back). And normally, whenever the table data model is changed, it would call its fireXXX methods to notify the JTable to update itself. You need to see if that happens in this case.


I can't figure out whether a fireXX is being called on the JTable, not quite sure how I would go about doing that.

What I did notice though whilst debugging is that the JTable binding is actually an IndirectList which is a separate entity than the List used to originally bind the JTable and this is what isn't being updated.

The changes are immediate in the JTable when editing the JDialog so the property changes on the object being edited must be firing correctly. I've read that it's possible to get the table to redraw by accessing the TableModel and firing a FireTableDataChanged event. The problem is though I can't access the table model because it's in a private class and I get a BindingTableModel cannot be cas't into DefaultTableModel.

Thanks for the replies.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have myself only used the samples for a basic understanding. Will try to dig deeper to get answers on these.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I looked at the code. Basically, the list is being added to and removed for the table to refresh. So, if you are doing a rollback, call em.refresh() for that entity and then remove that object from the list and add it again to the list. This should refresh the table.
 
Ste Hutton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranganathan Kaliyur Mannar wrote:Hi,
I looked at the code. Basically, the list is being added to and removed for the table to refresh. So, if you are doing a rollback, call em.refresh() for that entity and then remove that object from the list and add it again to the list. This should refresh the table.


Thanks, I managed to get this working. I just rebuilt the part of the application exactly the same as previous and for some reason it worked.

Strange.

Thanks for your time.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!