Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Data binded form not working as per book example
I'm following the examples here and unless I'm missing something or making a dumb mistake, I can't make it work. I do get the bean form but I don't get the custom fields generated from my FormFieldFactory. I debugged the code and the code within the factory is never called. I'm trying to figure out why.
Here's the relevant code;
final Form form = new Form();
// Set form caption and description texts
form.setCaption(getInstance().getResource().getString("message.requirement.create"));
// Set the form to act immediately on user input. This is
// necessary for the validation of the fields to occur immediately
// when the input focus changes and not just on commit.
form.setImmediate(true);
// Create a bean item that is bound to the bean.
BeanItem item = new BeanItem(new Requirement());
// Bind the bean item as the data source for the form.
form.setItemDataSource(item);
//Set the order of fields to display as well as which fields to hide
ArrayList<String> fields = new ArrayList<String>();
//Required fields
fields.add("uniqueId");
fields.add("description");
fields.add("requirementTypeId");
for (String field : fields) {
form.getField(field).setRequired(true);
form.getField(field).setRequiredError(
getInstance().getResource().getString(
"message.required.field.missing").replaceAll("%f", field));
}
//Non required fields
fields.add("notes");
form.setVisibleItemProperties(fields);
form.setFormFieldFactory(new FormFieldFactory() {
@Override
public Field createField(Item item, Object propertyId, Component uiContext) {
// Identify the fields by their Property ID.
String pid = (String) propertyId;
if ("uniqueId".equals(pid)) {
TextField textField = new TextField(getInstance().getResource().getString("general.unique.id"));
textField.addValidator(new Validator() {
@Override
public void validate(Object value) throws InvalidValueException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean isValid(Object value) {
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("id", value);
parameters.put("product",
new ProductJpaController(
DataBaseManager.getEntityManagerFactory()).findProduct(currentProduct.getId()));
List<Object> result = DataBaseManager.createdQuery(
"select r from Requirement r where "
+ "uniqueId=:id and productId=:product", parameters);
if (result.isEmpty()) {
return true;
}
return false;
}
});
return textField;
}
if ("description".equals(pid) || "notes".equals(pid)) {
return new TextArea(getInstance().getResource().getString("general.description"));
}
if ("requirementTypeId".equals(pid)) {
Select select = new Select(
getInstance().getResource().getString("message.requirement.type"));
for (Iterator<Object> it = DataBaseManager.namedQuery("RequirementType.findAll").iterator(); it.hasNext();) {
RequirementType rt = (RequirementType) it.next();
select.addItem(rt.getName());
}
//Allow to create new ones
select.setNewItemsAllowed(true);
return select;
}
return null;//Invalid field
}
});
...
All fields show up as TextFields (the defaults)
Any ideas/hints? This is on Vaadin 6.8.0
Try setting the FieldFactory prior to setting the datasource. If I remember correctly, order does matter in this case.
Tobias Demuth: Try setting the FieldFactory prior to setting the datasource. If I remember correctly, order does matter in this case.
That did the trick! Thanks a lot!