Solved

JTable and DefaultTableModel usage?

Posted on 2001-08-28
11
833 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 87
ejb example issues 3 55
How to convert from xls to xlsx using java 7 95
Eclipse with various Java releases 7 51
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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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