Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable ClassCastException: Add rows dynamically

 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

Heres what I wrote to implement a simple JTable.



Can anyone tell me why this code gives a the following runtime error of ClassCastException?

---------- Run Java File ----------
java.lang.ClassCastException: java.lang.String
at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:238)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:194)
at TablePopulation.<init>(TablePopulation.java:36)
at TablePopulation.main(TablePopulation.java:49)
Exception in thread "main"
Output completed (3 sec consumed) - Normal Termination
[ March 17, 2006: Message edited by: Sherry Jacob ]
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sherry,
The first parameter should be a Vector of Vectors for setDataVector. Try add the "rows" returned to a new Vector and use it as the first param of setDataVector.
It makes sense coming to think that a table is a two dimension presentation of data.

The cause of your problem is that with your initializing of the table model, a String was being tried to cast into a Vector thus the error.

Good luck.
[ March 17, 2006: Message edited by: Ben Zung ]
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, show() is a method inherited from Window which has been deprecated. You may want to stay away from it -- or rather all those deprecated. Use setVisible() works.
Also, you probably want to pay some attention to type safety. ie

I would parameterize cols like this


Have fun!
[ March 17, 2006: Message edited by: Ben Zung ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic