• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ResultSet to TableModel (using ArrayLists)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guys.
I have this code that takes a ResultSet and returns a TableModel. before i had Vectors instead of ArrayLists and it worked fine, but a friend recomended that i should use Arraylists instead.
Now the Problem how can i get the DefaultTableModel to accept ArrayLists instead of vectors?




Any constructive comments are greatly appreciated

//Christoffer

Edit: I Think this is a Swing problem if not could any kind moderator please move it to the correct part
 
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did something similar. I retrieved an ArrayList from my database, and then turned that ArrayList into an Object[][]. It worked out great. I made it so that the object that i was retrieving out of the database had its own toObjectArray method (called in a foreach loop) where it said where all the information should be, (in other words returned an array of its members), and then those arrays were then added to an ArrayList<Object> and then called the toObjectArray method on that ArrayList. Worked wonderfully.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator









I am quite proud of it. Hope it helps
 
Christoffer Killander
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Greg for your help
It got me further but i still have some trouble. (see the attached image)

The code right now:


Got any idea of how i can fix this ?

//Christoffer
TableError.JPG
[Thumbnail for TableError.JPG]
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must admit that I am a bit stumped at the moment. The picture looks like you are giving and object[][][] as a datamodel, but I cannot find that in your code. I need to look at it in the morning with fresh eyes. As an aside, i would give you the recomendation to put more effort into naming your variables. For example newRow could be dbRowData and it would make it much easier to read. If i think of anything else, I'll let you know, otherwise, I need a bit of time to look at the source code a bit further.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now the Problem how can i get the DefaultTableModel to accept ArrayLists instead of vectors?



You can't use the DefaultTableModel with ArrayLists.

You need to create a custom TableModel.

One approach would be to copy the code for the DefaultTableModel and the change the references to a Vector to be references to an ArrayList.

Another approach would be to create a more generic TableModel that can use any List. Both Vectors and ArrayLists implement the List interface, so create a model that implements the interface not the concrete implementation of the List. This is a little more complicated, but more flexible. For a working example of this approach you can check out my implementation of a List Table Model.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code from above makes no sense. There is no reason to copy the data from the ResultSet to an ArrayList and then convert the ArrayList to an Array which you use to create the DefaultTableModel. The DefaultTableModel will just copy the data from the Array to a Vector anyway so you haven't saved anything. So just keep the code the way it was before.

The only time you would do this type of conversion is if you have some external process that is passing an ArrayList to your code.

Again, as I mentioned in my previous post, if you actually want to store the data in an ArrayList, then you need to create a custom TableModel.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would look at line 26, and although it does not seam likely, I would write rowss[i] = rows.get[i] (or) rows.get[i].toArray(). I dont know if this will help, but it is an idea.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob has the best idea for your particular solution. I am working with methods from multiple classes at one time where the conversion to an Object[][] is easy, you are trying to do everything with just 1 method. It doesnt really make much sense to do it the way you are.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One other thing. Why was it said that an ArrayList is better than a Vector? If it was already working, why re-invent the wheel?
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why was it said that an ArrayList is better than a Vector?



A Vector is synchronized but an ArrayList is not. Therefore it is advised to use ArrayList when threading is not an issue so you don't have the overhead of the synchronization.

Will you notice a performance improvement when using a JTable? I doubt it, but most code these days is written using ArrayLists, not Vectors. If you ever do need to synchronize an ArrayList you can use the Collections.synchronizedList() method.

 
Greenhorn
Posts: 28
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello you must update like in 27th line.

Christoffer Killander wrote:Thanks Greg for your help
It got me further but i still have some trouble. (see the attached image)

The code right now:


Got any idea of how i can fix this ?

//Christoffer

 
The City calls upon her steadfast protectors. Now for a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic