wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to update JTable Data??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to update JTable Data???" Watch "How to update JTable Data???" New topic
Author

How to update JTable Data???

Pervez Ahmed
Greenhorn

Joined: Jul 18, 2001
Posts: 19
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

Joined: Feb 25, 2002
Posts: 560
Are you calling fireTableDataChanged() from your table model methods?
Pervez Ahmed
Greenhorn

Joined: Jul 18, 2001
Posts: 19
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Pervez Ahmed
Greenhorn

Joined: Jul 18, 2001
Posts: 19
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

Joined: Feb 25, 2002
Posts: 560
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Feb 25, 2002
Posts: 560
Mark,
My model extended AbstractTableModel. That's why I needed it. Yours probably extended DefaultTableModel, right?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Nope mine extends AbstractTableModel too.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to update JTable Data???