Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes can't get JTable(vector, vector) constructor to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "can Watch "can New topic
Author

can't get JTable(vector, vector) constructor to work

luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
Hi everyone,
All i'm trying to do is make a JTable with the data from two vectors i have.
I have one vector containing all string objects that is for my row data, and another vector that contains my column names.
It seems like it should be simple enough to just now use those two vectors in the JTable constructor, but when i run my code i get these errors:

java.lang.ClassCastException
at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:238)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:194)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:131)
at javax.swing.JTable.<init>(JTable.java:403)
at NetNegotiator.makeTable(NetNegotiator.java:187)
at NetNegotiator.<init>(NetNegotiator.java:246)
at NetNegotiator.main(NetNegotiator.java:349)

Im really quite new with using JTables, and im stumped as to why this isn't working. I had it set up before using the JTable(object[][],object[]) contructor and then passing it data was fine, but i recently changed the code so that i read an XML file and extract the contents into a vector(which works for sure), and wanted to change my table so that it uses this data from the vector now instead.I hope i havn't confused anyone.
If i could get some help it would be much appreciated,thanks for the time and patience.
-luc


National Research Council<br />Internet Logic Department
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

Check the following from the Java API Documentation for javax.swing.JTable and see if you have your arguments correct:

public JTable(Vector rowData, Vector columnNames)

Constructs a JTable to display the values in the Vector of Vectors, rowData, with column names, columnNames. The Vectors contained in rowData should contain the values for that row. In other words, the value of the cell at row 1, column 5 can be obtained with the following code:

((Vector)rowData.elementAt(1)).elementAt(5);


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
Originally posted by Joe Ess:
Check the following from the Java API Documentation for javax.swing.JTable and see if you have your arguments correct:

Hey joe,
yes they are correct.Ill show you an example of how i am doing this and isn't even working when i make two two vectors from scratch, so i know its not in my xml parsing method.

Vector rowData= new Vector(6);
rowData.add("weee");
rowData.add("weee");
rowData.add("weee");
rowData.add("weee");
rowData.add("weee");
rowData.add("weee");

Vector columnNames =new Vector(6);
columnNames.add("Company");
columnNames.add("Date");
columnNames.add("Deal Reached");
columnNames.add("Information Given");
columnNames.add("Rewards");
columnNames.add("Pleasure Rating");

negotiationTable = new JTable(rowData, columnNames);

///this is exactly how i tested it in the simplest case and its is still giving same errors.

Can you see anything wrong, like the constructor takes two vectors how figin hard can this be! lol
anyways any comments would be greatly appreciated.
thanks
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

That's exactly what I thought you were doing. Read the documentation closely:

Constructs a JTable to display the values in the Vector of Vectors. . .

A Vector of Vectors, not a Vector of String objects. Imagine if you had more than one column in your table. How would you store those entries in a Vector of String objects? Can't be done. A Vector is one-dimensional. We make it two-dimensional simply by saying that each "row" in the data Vector will be a Vector of row entries.
BTW, arrays in Java work the same way. In C and other languages you can declare a 2-D array, like a matrix, with all the second dimension arrays the same size. In Java, 2-D arrays are actually "arrays of arrays", so each of the arrays in the array need not be identical in length.
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
Damit, although i haven't figured out the problem with my real data...i figured out how to make it work with the fake data i made up there.I failed to read the part that says "vector of vectors of objects" which at first confused the hell out of me anyway, i guess i just have to do exactly that and find some way that i can separate my real data into multiple vectors then put those vectors inside another vector, then put that vector in the constructor...seems rather pointless to me ..but hey what do i know.
Again if there are any more suggestions please feel free.
-luc
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
thanks joe i realized that u posted that while i was typing that message, after i had already sent it.I think i should be good to go thanks for the help.
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
hi back again after editing my method to make a vector of vectors.
Agian the context is,I have an xml file that looks like so:


And i am using JDom methods to parse it in java.It seems that when i go to add this "vector of vectors" to my JTable its only adding the first packet to my JTable 3 times (that being the number of packets in my xml file).
Hopefully you can still answer this question in this thread if not maybe u can direct to one where i can find some help, but if u dont mind taking a look at my code,here it is

thanks in advance.
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
two things to note..
where it says obj= it.next(), its suposed to be Object obj= it.next();
also in case u are thinking maybe somethinis wrong with the way im puting that returned vector in my table, this is how i called it.
Note that this code is in my GUI constructor.


like i said its printing the same information out three times in the table.
thanks.
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
yah i figured out a couple of things i was doing wrong

1:wasnt removing all the elements after each iteration.

2:before you can actually remove all those elements you have to make a clone of that vector and put it in a new temp vector.

3:add this temp vector to the "Vector of Vectors", then you can go ahead and remove all the elements in the orignal one that i called packetVec.

--this infact fixes the problem, so i didnt give any chance to you other genius' to work on this but its infact part of my job so i have been working on it all morning :S

Hope anyone who was following this thread learned something from it,because i defintly did.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't get JTable(vector, vector) constructor to work