File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Arrays, Vectors and Tables - Adding and Deleting objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Arrays, Vectors and Tables - Adding and Deleting objects" Watch "Arrays, Vectors and Tables - Adding and Deleting objects" New topic

Arrays, Vectors and Tables - Adding and Deleting objects

Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
Hello Again.. <-----------Greenhorn smile

I have an array that is put into a vector as an 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.

Thanks in advance for your advice!

Deb AKA Sleepless until this class is over...
K. Tsang

Joined: Sep 13, 2007
Posts: 2229

Hello Debra,

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.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
I have an array that is put into a vector as an 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.
Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
Rob Camick wrote:
I really don't understand the assignment.

Hi Rob,

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.....

As always, much thanks and gratitude, in advance.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
your Vectors are used to create the table's model.
you should now be dealing with that model.

as an example, from the code you posted in your editable table post (full working example),
add this line in buildGUI(), just before setting the frame to visible

you'll need to change the renderer setting, otherwise it'll error

//if( column >2 ) return dcr;//return decimal format columns 3-7
if( column >2 && column < 7) return dcr;
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Arrays, Vectors and Tables - Adding and Deleting objects
Similar Threads
performance while executing loops
Ordering in ArrayList/Vector
ArrayList Vector Collections Array
List creating
Vector or Array List which one is better for WebApplication?