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
Edit: I Think this is a Swing problem if not could any kind moderator please move it to the correct part
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.
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.
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.
Joined: Jun 13, 2009
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.
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.
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?
Joined: Jun 13, 2009
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.