This week's book giveaway is in the Agile and Other Processes forum.We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!See this thread for details.

JTable doesn't refresh after booking record

Theo van Loon

Ranch Hand

Posts: 71

posted 12 years ago

In the JTABle i use an AbstractTableModel when booking i update the datafile. That works just fine. But the JTable doesn't show the new results only when i exit and restart the application. In my update method i call :

This worked at first but now it doesn't seem to work anymore... Can someone give a clue?

Satish Avadhanam

Ranch Hand

Posts: 697

posted 12 years ago

Hi Theo, if you are using TableModel, try fireTableDataChanged(). Or you can just create a new JTable in the update too. Either way, table will be updated. GoodLuck.

Theo van Loon

Ranch Hand

Posts: 71

posted 12 years ago

But in the update of my AbstractTableModel i call the fireTableDataChanged() and the notifyObservers calls the update()....

Satish Avadhanam

Ranch Hand

Posts: 697

posted 12 years ago

Hi Theo

Originally posted by Theo van Loon: But in the update of my AbstractTableModel i call the fireTableDataChanged() and the notifyObservers calls the update()....

Here's how I did. In the controller, whenever you search with new criteria the results from the backend are returned in a arraylist or something like that right? What I did is used a function called setResult in TableModel subclass. In the controller I call this method of TableModel with the new arraylist and then invoke fireTableDataChanged. It worked fine.

Theo van Loon

Ranch Hand

Posts: 71

posted 12 years ago

Originally posted by Satish Avadhanam: Hi Theo

Here's how I did. In the controller, whenever you search with new criteria the results from the backend are returned in a arraylist or something like that right? What I did is used a function called setResult in TableModel subclass. In the controller I call this method of TableModel with the new arraylist and then invoke fireTableDataChanged. It worked fine.

Satish thanks for the help!! This is the code :

Can i safely say that when an exception occurs in my book method it will never update the tablemodel? Thanks a lot!!

Satish Avadhanam

Ranch Hand

Posts: 697

posted 12 years ago

Hi Theo

Originally posted by Theo van Loon:

Can i safely say that when an exception occurs in my book method it will never update the tablemodel?

As such from the above code, if an exception occurs at dataAccess.book(..) method, the next line will not execute and so the tablemodel will not be updated in my understanding.

Thanks a lot!!

Here's the flow in my controller class which updates the table.

This will update the table. Also you can use something like this instead of above two lines.

Good Luck.

Theo van Loon

Ranch Hand

Posts: 71

posted 12 years ago

Satish last one : do you call fireTableDataChanged() in the controller class or can you also call this in the update method of the TableModel class :

Thanks!!

Stephen Galbraith

Ranch Hand

Posts: 90

posted 12 years ago

In fact I think this method is preferred, controller tells teh model to change it's data. Model tells registered listeners that are interested in that data that it's changed. What would happen if you had more complicated logic in the model that may decide not to update the views (if, for example the data is the same). In the controller it MUST always call the update, but if this is from the model then it can process the data and decide. Steve

SCJP 1.4, SCJD, SCWCD 1.4

Satish Avadhanam

Ranch Hand

Posts: 697

posted 12 years ago

Hi Theo, sorry if I was'nt clear before. I called fireTableDataChanged() in the controller only. It seems that you are calling them in the TableModel method. I don't know if it works or not, but I called it from the controller itself. If you want to follow the second approach here's what I did.