• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing JTable with new data displays only last row, repeatedly  RSS feed

 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application I have a feature to go to the previous or next week. The data from that (new) week has to be displayed. I start with removing all rows from my table. Next I get data from my database based on the first day of that week (the Monday) and insert (or add) the rows to the JTable. However, the result is not as I expect it. I get the last row I read from the database repeated.



As you can see I generate logging. The logging is as I expect it:

But when I look at the result on screen I see:


Edit: What is going wrong (besides my image not displaying)?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Remove all rows of current model


You can just use:



I get the last row I read from the database repeated.


You are adding the same Vector to the TableModel 6 times. So your TableModel contains 6 references to the same Vector

You need to create a new Vector for each row.

 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
// Remove all rows of current model


You can just use:



Am I correct that all rows in the model will be ready to be garbage collected after setRowCount(0); (when the rows are not used anywhere else in my program)?

Rob Camick wrote:
I get the last row I read from the database repeated.


You are adding the same Vector to the TableModel 6 times. So your TableModel contains 6 references to the same Vector

You need to create a new Vector for each row.



That is rather a face palm moment. So, if you are using collections, you should always create a new collection inside a loop?

 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I correct that all rows in the model will be ready to be garbage collected after setRowCount(0);


Well, the setSize() method of the Vector class ultimately gets invoked. When you remove a row from the model the remove() method of the Vector class gets invokes. So if those methods doe their job properly then yes.

You can always look at the source code of the Vector class if you want to see the details. In Windows the source comes in src.zip in the root installation of the JDK.

So, if you are using collections, you should always create a new collection inside a loop?


Has nothing to do with Collections. If you want 6 objects you need to create 6 instances of the object.

Taking a second look at your code, the better solution would probably be to have your "getRow(...)" method actually return a new row of data. There is no need to pass in the row parameter.

Having said that you sometimes need to know how the API works. In the case of a DefaultTableModel you can also insert an Array of data into the TableModel. In this case the data is actually copied from the Array to a newly created Vector and then the Vector is added to the TableModel, since the underlying data structure of the DefaultTableModel is a Vector of Vectors. So in this case the API does the work for you. But I would not assume this is the case.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OKay, Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!