Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Refresing a JTable  RSS feed

 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a system which allows the user to add some add into an Access database. There is a JTable that shows the contents of this database, but it doesnt show the new contents which has been added. I have tried using JTable's repaint() and revalidate() methods but it doesnt work.

I am only using a JTable. Is there a different way to achieve this? Can you please provide any examples if possible.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to refresh the table's data completely, not just repaint it.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You'll need to refresh the table's data completely, not just repaint it.

how can i do this?

thanks
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you fill your table's data initially?
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using beans technique so i have a method called displayCV60, which returns a JTable..the code is below:



I havent created any tablemodel or anything like that..

thanks rob
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so you know how to get the headers and data into Vectors. Now you must know that a JTable actually works with models. In your code, the JTable constructor has secretly created an instance of DefaultTableModel using those Vectors.

All you need to do is create another DefaultTableModel instance, and then call setModel on the JTable using that new DefaultTableModel.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so something like this?



Is this correct? if this is correct then can i just add this bit in the same function as above?

thanks
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leave out line 2. Otherwise your cvTable variable points to a completely new JTable that is not part of the GUI. Also, you are using the wrong DefaultTableModel constructor. This way it will be empty, and that's not what you want.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Man you know it by just looking at the code..thats exactly what i got nullpointerexception.
Is it something like

 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct, but you must first have (somewhere else) have created the cvTable instance and added it to your user interface.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. cvTable is a global variable that is defined at the beginning.

Still get a nullpointer exception. I have changed the return type to DefaultTableModel and set it to cvModel.

In the main code i have done:



The error highlights line 3. Done everything as mentioned still no luck.

 
Piyush Patel
Ranch Hand
Posts: 127
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to call again the method/fuction that shows or fetches data from databse after adding data to databse table.

that way you will have fresh data !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!