Skip to main content

Binding a list of javabeans to a table?

6 replies [Last post]
aaime
Offline
Joined: 2003-06-10

I'd like to know if the project, in the current state, is able to bind a list of javabeans to a table. It seems to me that this is not possible, but that this can be done with JGoodies binding.

Ideally, what I'd like is to get a list of beans, build a model telling only the class of the java bean and the fields that I want to be bound, provide hints about editability and column header labels, and attach to a table.

When editing is finished, I'd like to have a list of the beans that have been added, removed and changed during editing... is this science fiction for the moment?

Also, too bad JDNC and JGoodies binding are separated libs... (having a little of glazed lists would be great, too)...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dhall
Offline
Joined: 2006-02-17

>> Ideally, what I'd like is to get a list of beans, build a model telling only the class of the java bean and the fields that I want to be bound, provide hints about editability and column header labels, and attach to a table.

How about something like this: (it assumes a list of Items that have the five named properties)
[code]
List data = // initialized from somewhere
GenericTableModel model =
new GenericTableModel(Item.class, data);

// adds a read-only column for the object's id
model.addColumn(Integer.class, "ID");

// adds a read-only column for the object's name
model.addColumn(String.class, "Name");

// adds an editable column for the object's description
model.addColumn(String.class, "Desc", true);

// adds an editable column for the object's count
model.addColumn(Integer.class, "Count", true);

// adds an editable column for the object's price
model.addColumn(BigDecimal.class, "Price", true);

JTable table = new JTable(model, model.getColumnModel());
[/code]
This is from the GenericTableModel in jga, available at the links below. I demo'd this class last summer at JavaOne, and you can use it if you have the code/libraries in my incubator area on classpath.

This takes care of the binding of columns to fairly arbitrary properties (it also supports computed columns). There's no provision in this table model for adding/deleteing rows, but if you hook this up to an observable list (glazed lists might work although I've never tried it) then adds/removals should be OK.

Compiling a list of changes would be an additional step. It's doable -- the starting list should be cloned before making the UI available to the user.

Dave Hall
http://jga.sf.net/
http://jroller.com/page/dhall

netsql
Offline
Joined: 2004-03-07

Binding a List to a TableModel or a Map to a FormModel is a common thing to do with JDNC.
(I use List and Map as a remote Service DTO to call services, similar to GoogleApi)

I had to tweak JDNC code a bit, but was not a problem.
So.. just add Sandra.jar from SandraSF.com, it's based on JDNC binding and model, not JGoodies.
You can look at javadoc from home page in org.sandra.api.. and see that model API, and other things Sandra adds on for you. Like clean MVC and OO and Service Api and security and validation and... while you wash your hair, it paints the house!! ;-)

There is no need for all of us to write common thing sseparate, we just want our app to work. If you need a patch or more doc, I'd be glad to do it.

.V

aaime
Offline
Joined: 2003-06-10

Nice, I'll have a look at it :-)

Now, I hope extensions like that one can be retrofitted into the JDNC code base, since this is a quite common need...

netsql
Offline
Joined: 2004-03-07

The idea is that you would use Sandra w/ JDNC :-) .. and not eliminate Sandra attribution.

Here is a better link on your model. See init (List)
http://www.sandrasf.com/other/sandra/javadoc/org/sandra/model/ABaseTable...
(See, it extends JDNC table model ex)

There are other things Sandra does, like Services, remote DAO, Full Text Search, complex bus. validation; and tutorials, etc.
Like in Struts, there are many jars, ... that is what Sandra would be, a set of jars that you need to develop a JDNC app.(Sandra also suports Flex and Google API and... did I say it has a clean MVC?! :-) )

(It comes w/ JGoodies layout and look and feel which I think are standard).
Anyway, check out my sample app next week.
There are many SWT frameworks out there, and JDNC will catch up.

.V
ps: aaime - send me your email, I will send you a free book on JDNC.

aaime
Offline
Joined: 2003-06-10

> The idea is that you would use Sandra w/ JDNC :-) ..
> and not eliminate Sandra attribution.

Oh no, don't get me wrong, giving credit is important,
but if you look in the JDNC road map, something similar
to what I'm requesting is already planned. If Sandra
and JDNC don't collaborate, they will inevitably overlap, and that's not good...

> Here is a better link on your model. See init (List)
> http://www.sandrasf.com/other/sandra/javadoc/org/sandr
> a/model/ABaseTableModel.html
> (See, it extends JDNC table model ex)
>
> There are other things Sandra does, like Services,
> remote DAO, Full Text Search, complex bus.
> validation; and tutorials, etc.
> Like in Struts, there are many jars, ... that is what
> Sandra would be, a set of jars that you need to
> develop a JDNC app.(Sandra also suports Flex and
> Google API and... did I say it has a clean MVC?! :-)
> )
>
> (It comes w/ JGoodies layout and look and feel which
> I think are standard).
> Anyway, check out my sample app next week.

I'll do... just my 2 cents: make sure the architecture
supports swapping layers. For example, for persistence
and transactions, and web services I use Hibernate and Spring, with quite a buy-in in both of them (I started using Hibernate 2 years ago, for example). There are lots of libraries around, and lots of people already happily using them. What I need most is a better client side solution, so I hope to be able to extract what I need from Sandra without being forced to pick the rest.
Anyway, I'll have time to look into it during the holidays, so I'll let you know on the SandraSF forums.

> There are many SWT frameworks out there, and JDNC
> will catch up.

Yup, I hope so too.

> .V
> ps: aaime - send me your email, I will send you a
> free book on JDNC.

You already did once, check your inbox (but I'll gladly accept an updated copy) ;-)

netsql
Offline
Joined: 2004-03-07

I only added things that are missing. If something is added to JDNC that works good, I will remove.
So no overlap, it's an add on.
Think of JDNC as the Servlet and Sandra as Struts.
Yes you can code to Servlet, but Struts makes it better and MVC. (For example Sandra adds a ModelInterface common to table and form model. There are many usefull things, not just collection; there is a Javadoc). If something is overheaed or needs improvment, then let us know, and it will be fixed.

No, you can't extract what you need from Sandra and use it! You can either use Sandra or not. You can't extract things from JDNC and use it either. (or Hibreante and Spring). It's not a big deal to put a usefull jar in there that is supporetd by the comunity. Open source is provided to help w/ quality of code, etc. and not for people to steal code; there'd be no open source if that was the case. Why would you want to?
Yes Sandra has built in way to support Hibrenate and Spring, you do not have to use iBatis and Struts w/ it.
In any case Sandra I think gets used in addition w/ JDNC w/ no overlap and w/ support for things that JDNC does not. Like you would not extract from Struts just the action or just the form bean. And w/ Struts you can still code servlet api.

In anycase, I expect many JDNC based frameworks, Sandra is one, it may end up the best or not.

I hope it clears it up. I hope you and everyone tries Sandra jar and Sandra tutorials and if they like it that they use it w/JDNC. If anyone wants to use parts of Sandra, w/o Sandra attrbution; 1st they need to obtain permision.

.V