I have a FieldGroup that I setup – before any Item is set – along with the Layout of those fields. However, one of those fields varies depending on the actual Item set (via setItemDataSource). I’m looking for how to replace a given field in a fieldgroup/layout to another based on the item’s type (based on a property value the item holds).
That is, if Item is “type 1,” then the Field in the FieldGroup/Layout is a TextField.
If that Item is “type 2”, then the same Field in the FieldGroup/Layout is a NativeSelect. And in our case, other types of the Item will use different select boxes to control the content of the field.
When I initially create the layout/fieldgroup, I am just using a TextField placeholder. Then on setItemDataSource, I want to unbind the field and rebind using the new field, and then replace the old component in my layout with the new field component.
This works with a standard master-view relationship in which the user selects which record from a Table, and that Table then triggers a value change on the Table, and we use the selected row’s Item to then set the Item for the form.
But I get this exception:
Caused by: java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:711)
at java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:734)
at com.vaadin.data.fieldgroup.FieldGroup.setItemDataSource(FieldGroup.java:104)
at com.esignforms.open.vaadin.config.reportTemplate.reportField.ReportTemplateReportFieldForm.setItemDataSource(ReportTemplateReportFieldForm.java:464)
at com.esignforms.open.vaadin.config.reportTemplate.reportField.ReportTemplateReportFieldView.valueChange(ReportTemplateReportFieldView.java:107)
When handling the new Item, I am basically trying stuff like:
// Determine which fields have special output format specs. If the field is a File type, there is no output spec at all.
Field<?> outputFormatSpec = (Field<?>)fieldGroup.getField("outputFormatSpec");
if ( outputFormatSpec != null ) {
outputFormatSpec.discard();
vaadinUi.removeAllValidators(outputFormatSpec);
fieldGroup.unbind(outputFormatSpec);
}
followed by ....
fieldLayout.replaceComponent(outputFormatSpec, select); // select is a NativeSelect I'm replacing the orig field with
fieldGroup.bind(select,"outputFormatSpec");