Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a model for a JTable using Vectors

 
Joe Mastro
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class called and within the class a method called

I am trying to use the contents of the Vector<Contact> (which is the result set of a db call) to populate a model in order to be used for a table in a JFrame.

I am getting the error: java.lang.ClassCastException: contact.Contact cannot be cast to java.util.Vector and am unsure as to how to trouble shoot this. Any help is appreciated and the code is below.

 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Javadoc says it all - the data Vector should be a Vector of Vectors, each inner Vector containing the data of one row.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And so: you can't use a DefaultTableModel like that. There are two main alternatives:

(1) Write your own table model which is based on a List<Contact> -- a subclass of AbstractTableModel would be easy enough.

(2) Convert each Contact object to a Vector, and then you have a Vector<Vector> which you can pass to your DefaultTableModel.

Which you choose would depend on how the table is supposed to work. Is it editable? Or partially editable? Do you want to specify column types so that rendering of types other than String will look nicer? Do you want changes to the table model's data to result in changes to other entities? And so on...
 
Joe Mastro
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. When clicking on the JTable I want:

1. Edits to be disabled

2. I need columns to size to cell contents

3. When clicking on a row in the jTable I need to be able to reference a first column primary key (data is being loaded from a db table) that will eventually be used to access and update a record in a db table.

Which method is best suited for this.....#1 or #2 from your reply to my post. Thanks.
 
Nam Ha Minh
Ranch Hand
Posts: 504
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can find answers to your questions by looking at the Java doc for JTable class.
 
Joe Mastro
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like what I need to do is subclass AbstractTableModel for the functionality I need.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's what I would have done (and what I typically do) in that case. The DefaultTableModel is really only useful if you want a quickie way of displaying data in a table.
 
Stephan van Hulst
Bartender
Posts: 6323
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A separate matter: why are you using Vectors? They are deprecated, you should favor ArrayList.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because unfortunately a lot of Swing code uses Vectors.
 
Stephan van Hulst
Bartender
Posts: 6323
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I guess I never had the misfortune to use a lot of those methods
 
Joe Mastro
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. I'll give the subclassing a go and post back any issues I encounter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic