detachField from Form - bug??

Hello all,

I have found out on vaadin 6.5.2 what I think is possibly a bug. the method detachField on the Form removes a component from its parent. But imagine the scenario where we have two forms on the same window, built from the same Item and showing both different fields. I join these forms by the use of TabSheets. The problem here is that in the past i would remove all fields that I did not use on the current form. When building the second form all fields from the first Form are removed. But know with this code:

protected void detachField(final Field field) {
Component p = field.getParent();
if (p instanceof ComponentContainer) {
((ComponentContainer) p).removeComponent(field);

if I remove a field on the second form it is removed on the first form?!?! Shouldn’t this be

protected void detachField(final Field field) {

so that this field would only be removed from the current form and not from others?

Thanx a lot


Do I understand you correctly that you are placing the very same instance of a Field in two different forms? This is not, and has never been, supported by Vaadin. Have you created some kind of custom hack that removes fields from the first form and places them in the second?

Anyway, without a more detailed description of what you are trying to do and what is not working, I don’t think that this could be classified as a bug in Vaadin.


Guessing based on your post and Jonatan’s comment, even though the situation is not completely clear to me:

Do I understand correctly that you have two separate instances of Form with different subsets of fields, but perhaps in the same layout? In a layout that is not the layout of the Form? Does your field factory potentially return the same instance of a field for different instances of Form? Do you customize field location by overriding attachField()?

Do make sure you use setItemDataSource(Item item, Collection propertyIds) instead of setItemDataSource(Item item) to avoid creating the extra fields in the first place. This might solve your problem partly or completely, also depending on your field factory etc.

A Form does manage some layout aspects of its fields itself, including normally removing them from the layout when the data source is changed. It is possible to override attachField() to place them in some other layout, but this can sometimes cause tricky situations (e.g. indirect memory leaks if you are not careful) when reusing field instances between data sources on the same form or between different forms even if not shown at the same time. Also, you probably run into complications at some point if you are moving fields generated by a form from one place to another after the form is initially set up.

I am sure this could be improved and probably should be, but doing this correctly would require some effort reviewing the whole form and field life cycle and layout management instead of only patching one method somehow. I believe this will not happen before some rework of the Form API.


I see that I was not clear about what I did. I have created a custom FieldFactory that contains a Field cache. So in every creation of a Field related to a Property the same field is returned. As for all forms on the same window I use the same Bean, some fields are generated (or returned from the cache) everytime you call the formatPropertyValue. I override the attachField on each Form so that I can place the Fields where I want on the layout. Each Field will appear on one Form only as I use the removeItemProperty at the end of the attachField for unwanted fields. The problem was that fields that I use on the first form are returned by the FieldFactory cache on the second Form and when I try to detach them with the regular method they are removed from the First form. Now i overrode the detachField to try only to remove the component on the current layout, solving the problem that I posted here.

Even so the default detachField from vaadin can still raise problems for unaware developers that come from vaadin 6.4.x