This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with JTable and Vectors

 
Lars Tode
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Lars Tode
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic