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

How to update JTable Data???

 
Pervez Ahmed
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am a bit frustrated at this point. When doing a retrieve in my app, I am able to correctly populate my JTable with data. If I go in a second time and do another retrieve the data is correct, but it does not populate/refresh the JTable. I am using the AbstractTableModel and I have TableModelListeners on my Gui class. I can see the new data for a second or two in the JTable and then the table reverts back to the first requests' data. I also do a repaint() after each retrieve.
Any suggestions are welcome !!!
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you calling fireTableDataChanged() from your table model methods?
 
Pervez Ahmed
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sai,
I've tried calling fireTableCellUpdated() as well as fireTableDataChanged(). Is there a specific method I should be calling this from. Right now I'm calling it from the setValueAt().
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you pass the data to the JTable, which method are you using?
You shouldn't have to issue a repaint.
You can create a new instance of your table model with the new data, and then call setModel. like this.

Mark
 
Pervez Ahmed
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
I am creating a new instance of my data model just as you have mentioned. The data in the model is correct, but the JTable refuses to refresh itself with the new data.
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pervez,
I am not sure why your JTable is not refreshing with the new data. But you can check the items below. Obviously something is wrong.
1) When you create the table, put it in a JScrollPane
2) Create one instance of the table model and assign to the JTable during instantiation
3) When you get the new data, assign the new data to the table model. Below is the sample code:

Good luck!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sai what's fireTableDataChanged();
I didn't have that in my code and it displayed my data fine.
Hmm. I wonder what I did? I just did setModel and then it would display without firing anything.
Oh well, if it works for you with it, and doesn't without it, then you guys need it.
Mark
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
My model extended AbstractTableModel. That's why I needed it. Yours probably extended DefaultTableModel, right?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic