Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable not refreshing with fireTableDataChanged

 
Lily Tan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After values have been added into the database, the JTable does not display the new data unless I terminate and execute the program again.
Am I supposed to use fireTableDataChanged or newDataAvailable?

I'm using DefaultTableModel:


And the table should refresh after I click the button:



Thanks in advance!
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to add the data to the TableModel. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
 
Richard Valvona
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do this. The cell simply needs to be repainted. This is the code from within the extended JTable class and the model variable is the AbstractTableModel I'm using. This code is also applicable to your DefaultTableModel.

 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Valvona, welcome to the Ranch!

In a properly coded application, the model will fire its own updates to notify the view that its data has been changed. The JTable in turn will respond to those updates by repainting the dirty region.

Since you have used the model reference, it's apparent that you are calling that method from outside the model. That indicates a deficiency in your model implementation.

Oh, and did you notice that the question was asked last year?
 
Richard Valvona
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Darryl,

Thanks for your feedback. I will admit that I am still quite newish to Java programming despite having done a Masters and then doing work-projects for almost a year. JTables have been quite a challenging area I have found and it's something I'm slowly getting better at.

The reason I chose to store the model in the variable was due to it being a subclass of the main model and that constantly down-casting after using is meant to be quite bad for performance. It's always stored as this variable after calling .

You say that it automatically repaints the dirty region. If that's so then why would even need to exist? In the context of using it, I have made it so that pressing the "delete" button clears the text in a cell just as you would expect in an Excel spreadsheet. It wasn't updating before then so I needed to include it.

Yes, I was aware it's a year old. I couldn't find a similar answer elsewhere and know that people still look at old threads.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic