File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable Problem... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Problem..." Watch "JTable Problem..." New topic
Author

JTable Problem...

Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
Here is the scenario: I have a dialog that contains 2 JTables. Each table is alike (same number of rows and columns, instances of the same tableModel...). My application gets updated data 4 times per second: whenever an update occurs a method in my class is called to get the updates and write them to the table cells. I am using the setValueAt() method of the tableModel to change the cells in the table. Although the tables contain data of the same type, when my application is running the LEFT table should be getting data displayed in the lower half of the table and the RIGHT table should be getting data displayed in the upper half of the table.
The problem is that at run-time when the tables are being updated the RIGHT table (the table that gets updated last) displays properly, but the LEFT table does not. The LEFT table switches back and forth at a random rate between what it is supposed to show and what the RIGHT table shows. It is almost as if the LEFT tableModel is getting it's reference mixed up. If I put breaks in my code and step through it, the cells that are supposedly getting updated are correct. I have added printlns to the code to see what is happening and to my surprise the calls to System.out.println actually appear to help correct the problem. I should not need to add delays to my code to get this to work correctly, but that is what it is appearing I might have to do.
If I remove the RIGHT table from the dialog the LEFT table displays fine...
Any clues as to why one table is getting random repaints of data that is from another table?
Clueless...

Lon Allen
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Lon,
You must remember things are still happening sequentially no matter what it looks like. In your case, think of filling a queue with event requests. When a Swing component needs to be repainted or updated the event is placed into the queue. The queue is the same one that your update is pumping full at 4 times per second! The painting of components takes relatively longer than anything else. JTrees and JTables are the worst because they have so many sub components that require repainting ... you need to account for this when you are filling in the event queue. You might only be able to handle 2 times per second updates, etc.
Of course, you can always get a faster machine. I hear SGI makes some graphical screamers for around $80-$100K.
Regards,
Manfred.
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
Manfred,
First of all I have been corrected by the guy doing the data updates on the backend that I only get the updates at 1 Hz so I was wrong about the 4 Hz updates (our C++ GUI does updates at 4 Hz). I can see what you are saying, but I don't see how/why my LEFT table is displaying data being updated to the RIGHT table. I tried making separate tableModels to fix this problem but that didn't work either. The thing is that I have repeated this scenario on a few other panels so my problem shows up in several places. I even hardcoded some of the data just to prove this and sure enough the RIGHT tables data is showing up in my LEFT table even after I had created separate tableModels instead of separate instances of the same model. There has to be a way to fix this...

Lon Allen
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You might have to post some code. I have a GUI with 6 different tables and it doesn't get confused.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
what about using different threads?
chantal
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
The problem stemmed from using the same vector to initialize both table's rows using the addRow(vector) method. I had seen this before but it slipped my memory. Somehow using the same vector for both tables ties the tables together in a very undesireable way. Having separate loops to intialize the two tables instead of one loop intializing both has fixed the problem. Thanks to everyone that responded...
 
jQuery in Action, 2nd edition
 
subject: JTable Problem...