aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Synchronizing JTable with a non Observable<List> in Netbeans Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Synchronizing JTable with a non Observable<List> in Netbeans" Watch "Synchronizing JTable with a non Observable<List> in Netbeans" New topic
Author

Synchronizing JTable with a non Observable<List> in Netbeans

Ste Hutton
Greenhorn

Joined: Oct 08, 2011
Posts: 9
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

Joined: Oct 16, 2003
Posts: 922
    
    2

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...


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Ste Hutton
Greenhorn

Joined: Oct 08, 2011
Posts: 9
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

Joined: Oct 16, 2003
Posts: 922
    
    2

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

Joined: Oct 08, 2011
Posts: 9
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

Joined: Oct 16, 2003
Posts: 922
    
    2

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

Joined: Oct 16, 2003
Posts: 922
    
    2

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

Joined: Oct 08, 2011
Posts: 9
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

Joined: Oct 16, 2003
Posts: 922
    
    2

You're welcome.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Synchronizing JTable with a non Observable<List> in Netbeans
 
Similar Threads
JTable
JTable ........
JTable
JComboBox with enum
jtable