[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

JTable and DefaultTableModel usage?

Posted on 2001-08-28
11
Medium Priority
?
855 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:
Suggested Courses

649 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