adding additional Property to BeanItemContainer


I really found the BeanItemContainer great to use, however, there is something I don’t really understand about this container:

In my application, i want to display the order lines of customer orders. To represent the orders, I have a clean model composed of 3 classes: Order, OrderLine, OrderLineAttribute. One Order contains OrderLines, which contains Attributes.

When getting information about a particular customer, I populate the order and order lines in memory as Order beans. To display my order and order lines, I wrap them in dedicated OrderContainer and OrderLineContainer, which both extend BeanItemContainer. My problem occurs when for example I want to display the associated Account First & Last Name linked to an order in my Order table.

I create a table to display a summary of the orders, and I tight it to my OrderContainer which wraps Order Beans.
My Order Beans do not contain accountFirstName or accountLastName as properties: they contain an account property of type Account. And the associated account bean has properties of firstName & lastName.
My question is: how do I display the associated account firstName & lastName without modifying the Order Bean? I don’t want to polluate my Order bean by adding getAccountFirstname and getAccountLastName method to it, which would return this.account.getFirstName() and this.account.getLastName. Is there any way to add this generated property to the Container directly?

Thanks for your insight on how I should tackle that!


Hi, Yannick!

The simplest solution would be to create a delegate class around your Order class that forwards all requests to your Order instance and provides getters for Account First- & Lastname and use this delegate class instead of Order to populate the container.

Another way, requiring more work, would be to first extend BeanItem, providing handler code for special propertyIDs (say propertyID “lastname” causes the BeanItem to fetch the value from your Order bean) and then extend BeanItemContainer to make it construct your subclass of BeanItem instead of the default one.

I’d go for the first and simplest solution.