I have an array that is put into a vector as an Arrays.as list and then displayed in a JTable. For a school assignment, I am to add and delete objects from the array with a GUI. I am not quite sure where to go with this... it is alot of information to put together.
I think I should use a setSize method to adjust the size of the vector. Is this correct? Will a vector automatically resize the Array or do I need to convert the array to an arraylist? It seems like the vector acts as a go between for the arraylist.... but I am so new at this I could very well be wrong.
I am looking for some programming approach guidance as well as attempting to problem solve before I run into too many obstacles taking the wrong path.
You are using a JTable to display your results. That JTable uses the DefaultTableModel which uses a vector of vector for the data.
The source is from some array. You got the array to vector of vector part right? Now you need to put the data back into an array?
Vector is a collection class meaning add/delete items will change its size automatically, just like ArrayList - only Vector is synchronized or thread-safe. On the other hand, if you don't like handling vector of vector for the table model, I suggest using a custom table model by inheriting AbstractTableModel. Then you will need to override methods to get number of rows and columns among other things.
For the table model, you can use the fireXXX methods to control the output of the JTable. If in doubt check out the Swing tutorial.
I have an array that is put into a vector as an Arrays.as list and then displayed in a JTable.
Doesn't make any sense. Arrays.asList returns a List object. Sure you can add the List to a Vector and then add the Vector to the DefaultTableModel. But all you will see in the table is the toString() representation of the List. This will be a single cell in the table, which defeats the purpose of having multiple columns.
I am to add and delete objects from the array with a GUI
Again this doesn't make sense. Updates to the TableModel should be done through the TableModel, not to the Array directly. Any teacher who tells you otherwise doesn't know what they are doing.
Arrays are not dynamic. You can't grow an array. So I don't understand the "add" part of the assignment. If the point is to learn how to copy data from one array and copy it to another, then I fail to see how a JTable is involved.
It seems like the vector acts as a go between for the arraylist
A Vector has nothing to do with an ArrayList. The are two separate classes that implement the List interface. You can't create an ArrayList from a Vector or vice versa. You can copy the data between the two.
I really don't understand the assignment.
Joined: Jun 11, 2009
Rob Camick wrote:
I really don't understand the assignment.
It's probably me that doesn't understand what I am doing rather than you not understanding the assignment. I understand some very basic concepts about arrays and tables, but my understanding of vectors and their interactions with arrays is sketchy. My assignment in short is to create a method that will add objects and delete objects from this representation of objects. My babble about how it was constructed was to avoid posting the same code again to show how the data was constructed. I have included a partial of the code (which I am sure you have seen ad-nauseum) to clarify my babble. I am sure that an array is not "growable". My assignment is to create buttons to which I will be able to add and delete to this data. Being the newbie that I am, I would start from scratch and change my array to an arrayList so that its size is able to change. Since I don't understand vectors, I know enough to know this may not be necessary, since I am not familiar with vectors and their behavior. Will the vector store data that is added? Please forgive my ignorance. I have read the API docs on vectors and my interpretation is that it will store both my array and new items that I might add. I also know that I don't know enough to correctly interpret the API docs. Once I understand the direction necessary, I will spend hours and hours writing code that probably wont work, but learning more about Java. Then I will ask more dumb questions.....