wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Unable to refresh JTable after Updating data into database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Unable to refresh JTable after Updating data into database" Watch "Unable to refresh JTable after Updating data into database" New topic
Author

Unable to refresh JTable after Updating data into database

Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi All,

At first, when JTable is coming for the first time, it has been populated perfectly. But, after updating any existing data or inserting any new data the new result is not getting reflected into JTable. It contains the old result (displayed for the first time). Please find the code below.


I have checked & found that the ResultSet has been updated fine(data.add(d);). But, the JTable is not getting updated.

Please help me.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

I must say that the code you provided is not at all in "Java spirit". You should at least follow naming conventions if nothing else. Those variables look more like C/C++. I would also suggest you modify your code to avoid using Vector since it's marked as obsolete for some time now; instead you can use List of any kind.

Back to the topic, when you add new data to your collection, you should invoke

and that should resolve the problem you're facing (assuming that other parts of your code are working properly, since your code is not SSCCE so I couldn't test it myself).

Edit: I must notice that you constantly avoid using code tags in your posts. I understand that you want to mark parts of your code where you think the problem is, but instead of coloring it red and blue, just put code tags around and insert some comments if you want to turn our attention to a specific part of code.


The quieter you are, the more you are able to hear.
Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi,

Do you mean to put the code

tmodel.fireTableDataChanged();

after data insert into database or after JTable....???
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Invoke it each time you add new value to your JTable.
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155


- not, DefaultTaleModel has implemented all notifiers correctly

- JTable isnot added to the GUI

- do not to create any Object inside try - catch -finally

- all Object would be closed in finally block (connection, resultset, statement)

- prepare all Object before

- create JTable and its model as local variable, remove all rows before data are loaded in model

- dirty hack could be to create a new model, then to fill data to the model, add model to the JTable

Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi,

Thanks for your valuable time. But the issue is still not resolved. I think it will be better if you can see the situation I am facing. Shall I upload the project & all details of it so that you can judge better ??
Thanks.
Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi,

I have succeeded to update data in JFrame properly. But, when I move to a new JFrame from the previous one (by clicking a button in the old frame), the older one is still running in background.
I am following the 4 main hints provided in internet. They are:

1. frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
2. frame.setVisible(false)
3. frame.dispose()
4. frame = null

I am using JPanel for keeping the JFrame. When I update a value twice and move to the JFrame containing the JTable, it is flickering once and then getting visible. Again, if I go for update, the JFrame is flickering twice and then getting visible. In this manner, the number of flickering increases and it slows the application.

In console, I have found that the value is updated twice. For three time, thrice this happens. Although, I am getting the correct output in my JTable (inside JFrame), but my point of view is for larger data the application will be extremely slow.

Please let me know if there is any way to completely destroy a JFrame when I am moving to a new one (so that the I can re-create the older one....and run the application in a faster way)...
Thanks.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Please let me know if there is any way to completely destroy a JFrame when I am moving to a new one (so that the I can re-create the older one....and run the application in a faster way)...

generally, there is never a need to destroy/re-create the frame.

*all* of your db query stuff needs to be done in the background, not on Swing's EDT.
Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi Michael,

In the background means in ActionListener section I need to do all the db stuff..???
Thanks.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
means a separate thread or a SwingWorker etc.

Swing's thread (EDT) is for listening and
painting. Any long-running task (db query)
just slows it all down.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

A lot of responsibilities in one class (even though it's a GUI class). I would suggest you change design of your classes a bit.
One of the solutions would be to define your own table model (e.g. as extension of DefaultTableModel) and provide methods for manipulating (CRUD) data from your database, and nothing else. On the other side, your GUI class would be responsible for what it should be in the first place - displaying data and interacting with user.
Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi,

Yes, I have used SwingWorker. It's great. Thanks a lot.

But, still the flickering continues as I have described before. In my JFrame, there are JPanel. Within JPanel, there are only JButton, JLabel. Is there any issue with JPanel ??

Also, after doing 1 update to database I have put some characters in System.out.println in order to test how many times it is happening for one single click in the JButton. But, with my surprise, I can see the message is coming in console many times instead of one.
Could you please allow me to know if this is a charactiristic of Swing or I am doing something wrong.


Thanks.
Subhrangshu Samanta
Greenhorn

Joined: Jul 30, 2012
Posts: 14
Hi Kemal,

Thanks a lot. But can you please give me some light about the display of message many times for one button click in console.
Thanks
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

Michael already told you: don't create a new frame every time. Update the display in the existing frame.

And none of these are really meaningful in the present context.
1. frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
2. frame.setVisible(false)
3. frame.dispose()
4. frame = null


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to refresh JTable after Updating data into database