Solved

JTable and DefaultTableModel usage?

Posted on 2001-08-28
11
811 Views
Last Modified: 2007-12-19
Using ForteCE, dropped a JTable  in a dialog and trying to fill it
with some sample data.  Here is my code to add data:

            // Set the default table headers
            Vector vecTblHeader = new Vector();
           
            vecTblHeader.add( "Head1" );
            vecTblHeader.add( "Head2" );
            vecTblHeader.add( "Head3" );
           
            Vector vecTblData = new Vector();
            vecTblData.add( "Data1" );
            vecTblData.add( "Data2" );
            vecTblData.add( "Data3" );
           
            DefaultTableModel tblModel;
            tblModel = new DefaultTableModel( (Vector)vecTblHeader,
(Vector)vecTblData );
            tblProjectInfo.setModel( tblModel );

 
the error:
java.lang.ClassCastException: java.lang.String
on both of them.  Seems to point to the following code:
            tblModel = new DefaultTableModel( (Vector)vecTblHeader,
(Vector)vecTblData );


looking at the api this is the constructor I want to use:
DefaultTableModel(Vector data, Vector columnNames)

Any ideas on what is going on or how to fix it?

Thanks
0
Comment
Question by:borg48
  • 6
  • 3
  • 2
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6434757
Can u post the stack trace for the exception?
0
 
LVL 92

Expert Comment

by:objects
ID: 6434761
Does your table have custom renderer, or any other customised behaviour?
0
 

Author Comment

by:borg48
ID: 6434827
This is the error it gives me:
the error:
java.lang.ClassCastException: java.lang.String
on both of them.  

And if I step through the code it errors on this:
tblModel = new DefaultTableModel( (Vector)vecTblHeader,
(Vector)vecTblData );
0
 
LVL 92

Expert Comment

by:objects
ID: 6434887
If you can post the exception stack trace we'll know exaclty what line the ClassCastException is occurring on.
0
 

Author Comment

by:borg48
ID: 6434973
Ok here is what prints when i catch the exception:
catch( Exception err )
System.err.println( err );
err.printStackTrace();

java.lang.ClassCastException: java.lang.String
java.lang.ClassCastException: java.lang.String
at javax.swing.table.DefaultTableModel.newRowsAdded( DefaultTableModel.java:261 )
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:206)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:133)
at PMACT.BuildProjectTable(PMACT.java:549)
..
..
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 6435531
DefaultTableModel expects the data vector to be a Vector of Vectors (not Strings).
ie. a Vector for each row.
0
 
LVL 1

Accepted Solution

by:
yaseransari earned 40 total points
ID: 6435566
Hello borg48,
             If you look at the documentation of the DefaultTableModel class you will see that one of  its constructor has 2 parameters
1. Vector of Data :- This Vector should actually be a vector of Vectors because the table is 2 dimensional with each entry accessed with a row,col pair for example (0 ,0)(0,1) .

2. Vector of column names :- This is a plain vector of column names

Now your code is passing a simple vector instead os a vector of vectors and this is the problem .
Because of this when the DefaultTableModel tries to access the entries of your "vecTblData" it gets a String instead of a Vector which it expects and hence it gives the ClassCast Exception.
I suggest the folowing

Vector rowData = new Vector();
rowData.add( "Data1" );
rowData.add( "Data2" );
rowData.add("Data3");

vecTblData.add( rowData );

Apart from this the tblHeaderVector is fine.

This would display  a single row.
Hope u get the point

Regards
Yaser Ansari        






0
 
LVL 92

Expert Comment

by:objects
ID: 6435577
Yaser,

Thanks for repeating what I just said, albeit I bit more long windedly.
0
 
LVL 1

Expert Comment

by:yaseransari
ID: 6435750
Hi Objects
         Your comment was not there while i wrote this answer . Else i wouldnt have commented at all. This is happening for the second time.God knows why this happens cause there was a time diff of a full 12 mins betn our comments .

Regards
Yaser Ansari  
0
 
LVL 92

Expert Comment

by:objects
ID: 6435791
No worries, thanks for the explanation :)
0
 

Author Comment

by:borg48
ID: 6437971
Thanks all for the support.  I gave the points to yaseransari b/c he have the sample which really explained it to me.  I really do appreciate everyones help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now