aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ResultSet to TableModel (using ArrayLists) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ResultSet to TableModel (using ArrayLists)" Watch "ResultSet to TableModel (using ArrayLists)" New topic
Author

ResultSet to TableModel (using ArrayLists)

Christoffer Killander
Greenhorn

Joined: Jul 06, 2011
Posts: 5
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
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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

Joined: Jun 14, 2011
Posts: 99










I am quite proud of it. Hope it helps
Christoffer Killander
Greenhorn

Joined: Jul 06, 2011
Posts: 5
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


[Thumbnail for TableError.JPG]

Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
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

Joined: Jun 14, 2011
Posts: 99

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

Joined: Jun 14, 2011
Posts: 99

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

Joined: Jun 14, 2011
Posts: 99

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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ResultSet to TableModel (using ArrayLists)