This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Is there any freebie for binding or connecting a List<> or other generics to a JTable model? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is there any freebie for binding or connecting a List<> or other generics to a JTable model?" Watch "Is there any freebie for binding or connecting a List<> or other generics to a JTable model?" New topic
Author

Is there any freebie for binding or connecting a List<> or other generics to a JTable model?

Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
I already got technojeeves's DbUtils, but I find it difficult to test stuff as a aftermath.
I want to, on the other hand, bind List<> (or other containers) to models instead.
Thanks
Jack
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Not sure I got your question right but a List is one dimensional while a JTable is for displaying two dimensional information. If you have the data in a two dimensional array then you can use the DefaultTableModel constructor . Or you can use the constructor where data is a Vector of Vectors.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
E Armitage wrote:Not sure I got your question right but a List is one dimensional while a JTable is for displaying two dimensional information. If you have the data in a two dimensional array then you can use the DefaultTableModel constructor . Or you can use the constructor where data is a Vector of Vectors.


Looks pretty easy!
Thanks
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
Excuse, I am back.
In the line of DefaultTableModel model = ...
lComplains cannot be converted to Object[][]
Thanks
Jack

E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
You can't cast a List to an Object[][]. Read the first reply I posted again.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
I didn't notice I could do something like this

But how can I create a vector of cols with initialized values?
I tried.

to no avail


Update:
Is it possible to avoid from adding items after their declarations?
like


the code would not be that neat to put data in code.
Thanks
Jack
Thanks
Jack
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Jacky Luk wrote:I didn't notice I could do something like this




Do read my first reply again. For that constructor data would need to be a Vector of Vectors which lComplains is not. You can't just display a List in a table. A list is 'one dimensional' while a table is two dimensional. Think about how the data is going to be displayed. How is the table going to know how many rows and columns to use?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

E Armitage wrote:Not sure I got your question right but a List is one dimensional while a JTable is for displaying two dimensional information. If you have the data in a two dimensional array then you can use the DefaultTableModel constructor . Or you can use the constructor where data is a Vector of Vectors.


I really wouldn't fully agree to this. Typically the List would be a List of custom objects. Each table row would be represented by one single list object detail. Every column would represent one of the attributes of the said object. Many a times, your UI will obtain the data from some DB. In such scenarios it is very easy and convenient to treat them as a List of objects. Compared to this manipulating an array of arrays or vector of vectors is complicated and error prone.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Maneesh Godbole wrote:
I really wouldn't fully agree to this. Typically the List would be a List of custom objects. Each table row would be represented by one single list object detail. Every column would represent one of the attributes of the said object. Many a times, your UI will obtain the data from some DB. In such scenarios it is very easy and convenient to treat them as a List of objects. Compared to this manipulating an array of arrays or vector of vectors is complicated and error prone.

The quote is from the API docs of DefaultTableModel stating what the parameters for using it should be.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

E Armitage wrote:
The quote is from the API docs of DefaultTableModel stating what the parameters for using it should be.

I know. I am not doubting or debating what you posted. My point was even though List is one dimensional and JTable is supposed to display 2 dimensional data, it is in fact easier to play around with the data using a List as compared to Object[][] or Vector of Vectors
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Maneesh Godbole wrote: it is in fact easier to play around with the data using a List as compared to Object[][] or Vector of Vectors
Thankfully, no one said that it was easier.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
Oh I see.
The issue's got to be
converting a list of objects (of certain class) into a Vector of Vector
What methods are able to do this job?
Or do I have to roll my own?


This seems to be faultless, hope to get it right this time?!
Thanks
Jack
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Rob has already done it:http://tips4java.wordpress.com/2008/11/24/list-table-model/

Edit the one that you really want is http://tips4java.wordpress.com/2008/11/27/bean-table-model/
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
E Armitage wrote:Rob has already done it:http://tips4java.wordpress.com/2008/11/24/list-table-model/


Really helpful, thanks
Jack
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
the one that you really want is BeanTableModel


Yes, or extend the RowTableModel as is demonstrated in the JButtonTableModel.java example code.

The ListTableModel is more like the DefaultTableModel in that it supports individual cells of data and not custom Objects and should not be used in this case.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
Rob Camick wrote:
the one that you really want is BeanTableModel


Yes, or extend the RowTableModel as is demonstrated in the JButtonTableModel.java example code.

The ListTableModel is more like the DefaultTableModel in that it supports individual cells of data and not custom Objects and should not be used in this case.


If I need to display the customer in 3 different ways, do I define 3 different beans, for similar items (Customer1, Customer2, Customer3)
which carry slightly different attributes? ehhh... sorry for being dumb...
And one more question, with ListTableModel, I can't do the following


The table displays an address representation ( I don't know what that is called ) of the object.
Thanks
Jack
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
I suggested you should NOT be using the ListTableModel.

If you want 3 different views of a customer then you create different views by removing TableColumns from the TableColumnManager of your JTable. This is the whole point of the Swing MVC design approach. The model hold the data and the view determines how the data is presented.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 590
Rob Camick wrote:I suggested you should NOT be using the ListTableModel.

If you want 3 different views of a customer then you create different views by removing TableColumns from the TableColumnManager of your JTable. This is the whole point of the Swing MVC design approach. The model hold the data and the view determines how the data is presented.


Okay, Rob, I would collect the data first in the beans and delete the unnecessary columns
Thanks
Jack
 
wood burning stoves
 
subject: Is there any freebie for binding or connecting a List<> or other generics to a JTable model?
 
Similar Threads
Social Network class hiearchy
Help with Arrays and ArrayLists
jgoodies binding
virtual methods
EL and custom tag