• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to refresh in real-time the JTable after inserting new data to the database?

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI there!

I have a code that updates the JTable only after the program is restarted, but how do I refresh the JTable without exiting the program? That must happen when I upload a file containing some data.

MainClass.class




That's the method that creates a table model:

MyTable.class



I want that to happen when I click on the upload file button.

MainClass.class




I guess there is something to do with the model?
 
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That depends a bit on what "refresh" means. If you want to change the entire contents of the JTable, then you need to create a new table model and assign it to the JTable, by calling the JTable.setModel(tableModel) method. It would be a good idea to change the code you already wrote so that it does that too, instead of creating a new JTable which is based on a newly created table model.

Or if you want to change only some of the contents of the JTable, then you need to change the existing table model. You would need to change the code you wrote; the code which runs after the database insertion needs a reference to the existing table model, and right now it looks like you don't save a reference to the table model anywhere.

As a general rule, if your code wants to update the data visible in the GUI, it should update the data models on which the GUI is based, rather than the components themselves. However simple components like JButton don't have a separate data model so you update them directly.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really understand what is the issue of the model. As I said if I exit the program and re-enter it the table is updated. The thing is I want to update it while I am using the program, let's say press a button and refresh the table. Could you explain in more detail what you mean by setting the model? If I am not wrong I already do that and it refreshes the table, well not when I want though. Cheers!
 
Paul Clapham
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you're happy with the way it works now then maybe there's no point in trying to rewrite your code?
 
Paul Clapham
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the other hand if you want to fix your code to reuse and reload a JTable:

You have a method which reads the database table and creates a table model. This is the code you need to reuse. Unfortunately it also creates and loads a JTable. This part must not be reused. So fix that method so that it reads the database table and creates [b]and returns[b] a table model. That produces code which can be reused as often as you like.

And the part of the code which creates a JTable? That should be done in the initialization phase, where you construct the GUI. Generally, constructing the GUI should be strictly concerned with creating Swing components and combining them so that the operator of the code will see them in the correct relationship. So creating a JTable would be part of that, as would creating the scroll pane to contain it and so on. The other thing to be included in that phase would be to call the method which loads and returns a table model from the database table, and then to call the JTable's setModel(table model) method to initialize the contents of the JTable.

Later, you're going to update the database and you want to update the JTable correspondingly. So you call the method which loads and returns a table model from the database table, then call the JTable's setModel(table model) method to fill the JTable.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi and thank you for advice,

I have tried to do some changes, but all unlucky:

That's the method for creating model:



And that's the method that is meant to update the table:



Next is the mainframe and the button action listener where I try to call the updating method:

Mainframe:



and inside the mainframe I have the button action listener:



I find strange it because if I comment out the first call of the update method inside the mainframe, clicking on the button does not do anything, even though it is supposed to add the table and all. Honestly, I try all the way around and can't make update the content. It only does it when I exit and re-enter the program. Is something to do with the Event dispatch thread?

Thank you!
 
Paul Clapham
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is all you need to do after inserting new data. Creating another JTable is the wrong thing to do -- you already have one.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my God! How I could be that blind. Thank you a lot, it solved the problem. God...
 
Paul Clapham
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing that doesn't help: the names of those methods. You have one method which is intended to create a new JTable for the initialization step, and another method which is intended to create a TableModel based on the current state of the database. You have called those methods "updateTable" and "myTable" respectively. The name of a Java variable or method or class should describe what that variable or method or class is meant to represent, or what its purpose is. So you have a method whose purpose is to create a JTable, but you give it a name which implies its purpose is to update a table. Likewise you have a method whose purpose is to create, or build, a TableModel from a database, but you give it a name "myTable" which explains nothing.

You may find that if you spend some time thinking about method names, you have more insight into what the methods are for and what they actually do. This could only be a positive thing.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right I perfectly know that but for some reason, I just neglect it, maybe because I know that I work alone, yet, is true that names of the variables and all the rest make it easier to understand the code for me and for others. I periodically find code online and sometimes I have blood coming out of my eyes seeing it, and still, I do the same mistake =). Thanks for heads up! Cheers!
 
reply
    Bookmark Topic Watch Topic
  • New Topic