Solved

JTable and DefaultTableModel usage?

Posted on 2001-08-28
11
820 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 97
Checkbox and ListView in Android Layout 4 67
Java Options in Zimbra Server 2 54
web application structure 18 77
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 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