• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable ClassCastException: Add rows dynamically

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic