Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excuse, I am back.
In the line of DefaultTableModel model = ...
lComplains cannot be converted to Object[][]
Thanks
Jack

 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't cast a List to an Object[][]. Read the first reply I posted again.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic