Solved

JTable and DefaultTableModel usage?

Posted on 2001-08-28
11
798 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
Comment Utility
Can u post the stack trace for the exception?
0
 
LVL 92

Expert Comment

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

Author Comment

by:borg48
Comment Utility
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
Comment Utility
If you can post the exception stack trace we'll know exaclty what line the ClassCastException is occurring on.
0
 

Author Comment

by:borg48
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
Yaser,

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

Expert Comment

by:yaseransari
Comment Utility
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
Comment Utility
No worries, thanks for the explanation :)
0
 

Author Comment

by:borg48
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

15 Experts available now in Live!

Get 1:1 Help Now