Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Creating a model for a JTable using Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating a model for a JTable using Vectors" Watch "Creating a model for a JTable using Vectors" New topic
Author

Creating a model for a JTable using Vectors

Joe Mastro
Greenhorn

Joined: Jan 05, 2012
Posts: 14
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

The Javadoc says it all - the data Vector should be a Vector of Vectors, each inner Vector containing the data of one row.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jan 05, 2012
Posts: 14
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

Joined: Oct 31, 2011
Posts: 498

I think you can find answers to your questions by looking at the Java doc for JTable class.


PM Me If You Want to get Urgent Help on Java Programming
Joe Mastro
Greenhorn

Joined: Jan 05, 2012
Posts: 14
Looks like what I need to do is subclass AbstractTableModel for the functionality I need.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Sep 20, 2010
Posts: 3647
    
  16

A separate matter: why are you using Vectors? They are deprecated, you should favor ArrayList.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Because unfortunately a lot of Swing code uses Vectors.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Hmm, I guess I never had the misfortune to use a lot of those methods
Joe Mastro
Greenhorn

Joined: Jan 05, 2012
Posts: 14
Thanks everyone. I'll give the subclassing a go and post back any issues I encounter.
 
GeeCON Prague 2014
 
subject: Creating a model for a JTable using Vectors