aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problems with JTable and Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems with JTable and Vectors" Watch "Problems with JTable and Vectors" New topic
Author

Problems with JTable and Vectors

Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
i want to create a dynamic JTable by using Vectors. I tried something in this way, but it doesn't work, no table is showing.
Here some parts of my code, that u can see what i mean :

At this point each Vector haven't any values, i've an own method to fill them:

I used something same for the col-names.
Have anyone an idea why that don't work?
greetx
Lars
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Lars,
Lets take an analogy to figure it out ...
1. I have an empty gas can.
2. I give you the empty gas can so you can fill your car's empty gas tank.
3. I then go to the gas station and fill my gas can.
4. Now, why won't your car start?
Sounds pretty simple doesn't it? No gas - no car start. The gas can is not connected to the car, so filling in the can won't help the car ... get the picture.
How about filling the vectors first and then sending them into the table?
Regards,
Manfred.
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
one question, how do u would make it?
You haven't data yet, but u should paint an table.
greetx
Lars
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
i added following to my code :

I do this before i make an instance of JTable.
But i now get following errormessage :
Exception occurred during event dispatching:
java.lang.ClassCastException: [Ljava.lang.String;
at javax.swing.JTable$1.getValueAt(JTable.java:415)
....
What's now wrong?
greetx
Lars
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
HI Lars,
No clue what you are doing wrong ...
If you want to start with an empty JTable then do it. Then you should do all your work with the DefaultTableModel (vector of vectors) which seems to be what you want anyway.

Regards,
Manfred
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
i've an idea but i'm not really sure.
May b java can't convert the vectors value 4 the table?
Is it true that a vector is an (kind of) dynamic array?
Mean that it has some kind of index like an array.
I've also another question, what i've heard Vectors are like pointers (c++), so what happen when i'm doing something like this :
String text = new String("Hello world");
Vector test = new Vector();
test.insertElementAt(text,test.size());
text = "";
Is the Vector now point at a null-value?
greetx
Lars
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
Originally posted by Lars Tode:
Hi,
i've an idea but i'm not really sure.
May b java can't convert the vectors value 4 the table?
Is it true that a vector is an (kind of) dynamic array?
Mean that it has some kind of index like an array.
I've also another question, what i've heard Vectors are like pointers (c++), so what happen when i'm doing something like this :
String text = new String("Hello world");
Vector test = new Vector();
test.insertElementAt(text,test.size());
text = "";
Is the Vector now point at a null-value?
greetx
Lars

I'm going to try to answer both of your questions.
About the table, I noticed in your addRow(..) method all you do is add the element to the Vector. Just because the JTable is displaying the Vector and you change the Vector then call repaint() doesn't mean that the JTable will get a fresh version of the Vector to render. The way that I would handle this situation would be: create the table, when you need to add something to both the Vector and the table then; add it to the Vector and use setValueAt(Object aValue, int row, int column) to update the table.
On to the whole Vector situation
Think about what has been added to the Vector, text, that is a reference to an Object. So if you change the Object that it points to then when you retrieve the reference from the Vector then it will point to the modified Object.
Hope this helps.
[ April 18, 2002: Message edited by: Ibrahim Hashimi ]

SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
so, if I've really understand, the Vector point after I removed the value from text at nothing (oder "").
Is there a possibility adding a value by my method, whitout changing or losing the value of newRow?
greetx
Lars
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with JTable and Vectors