PropertysetItem with ComboBox ???

Hi to all,
for my application I need to use PropertysetItem instead of BeanItem: the reason it that the forms are built dynamically according a data base, that describes field names, types, regex for validation and so…

The book of Vaadin does not contains many examples about it, it says [quote]
use the ready BeanItem adapter to bind the form to any JavaBean object. You can also use PropertysetItem to bind the form to an ad hoc set of Property objects, resembling a Map.
[/quote] …and then it start to explain the use of JavaBean :frowning:
There is a small example, but only with Textfields.

I wrote this code, that it works very well for textfield (for sure, it is exactly like in book of Vaadin) but it does not work for ComboBox, Select, etc…
The problem is the line in red; if I comment it, the ComboBox is correctly drawn, with its options…but it is not added to form data source so I cannot retrieve the values set by the user.
If I uncomment it, instead of a Combobox, a TextBox is drawn, with a default text inside it equals to ObjectProperty constructor parameter (in this case “”).

So PropertysetItem must be used in a different way in Combobox to make it works, unfortunately I have not found examples…Someone can help me ?

Thanks very much!


public abstract class DictionaryForm extends Form {
  protected final PropertysetItem formData = new PropertysetItem();
  [...]

    public DictionaryForm(List<ItemDictionaryObj> itemDictionaryObj, Layout formLayout) {
        super(formLayout);
        init(itemDictionaryObj);
    }
  [...]

private void init(List<ItemDictionaryObj> itemDictionaryObj) {
        for (ItemDictionaryObj obj : itemDictionaryObj) {
            ItemDictionaryObj.Type type = obj.getType();
            String ID = obj.getID();
            boolean obbligatory = obj.isObbligatory();
            if (type.equals(ItemDictionaryObj.Type.TEXTFIELD)) {
                TextField textField = new TextField(obj.getLabel());
                [...]

                this.addField(ID, textField);
                formData.addItemProperty(ID, new ObjectProperty<String>(""));
            } else if (type.equals(ItemDictionaryObj.Type.SELECT)) {
                ComboBox select = new ComboBox(obj.getLabel());
                [...]

                for (SelectBoxObj boxObj : obj.getList()) {
                    String itemID = boxObj.getValue();
                    select.addItem(itemID);
                    select.setItemCaption(itemID, boxObj.getLabel());
                }
                this.addField(ID, select);              
                [color=#ff0000]
[b]
formData.addItemProperty(ID, new ObjectProperty<String>("")); 
[/b]
[/color]              
            } else if (type.equals(ItemDictionaryObj.Type.CUSTOM)) {
                [...]

            }
        }
        this.setItemDataSource(formData);
    }
}

I found a workaround to this problem using a custom FormFieldFactory instead of calling directly addField(ID, select) from the form but I am still curious to know if there is a different solution…

hi enrico,

did you come across a solution to this? i am considering vaadin for the exact purpose you have listed above (create dynamic forms from field criteria saved in db), but i am really struggling to find the right solution.

thanks,

b

anyone have any ideas?

I’m also looking for the correct way to use a ComboBox with a FieldGroup, so that validation on my ComboBox will be functional.

Currently I am doing this:

@PropertyId(“messageType”);
private ComboBox msgTypeSel;

PropertysetItem item = new PropertysetItem();

// Code to initialize combo box

// Below line when uncommented breaks validation so that the form submission is immediately called
// item.addItemProperty(“messageType”, msgTypeSel);

// This line works as far as resulting in a working validator, but a null selection option is added
item.addItemProperty(“messageType”, new ObjectProperty(“Select Message Type Default”, String.class));

I’m guessing what I’m doing wrong include the following:

  1. Using PropertysetItem instead of a more appropriate class
  2. Using ObjectProperty for the ComboBox instead of a more appropriate implementation of Property

So, my ComboBox is almost properly populated with options, except there is an unwanted null selection option at the top, and validation works as expected.

Hi there,

i found an workaround in this thread:
https://vaadin.com/forum#!/thread/3183746

the workaround is to bind the Field direct with

ComboBox combobox = new ComboBox(keys.getName());
combobox.addItems("Io", "Europa", "Ganymedes", "Callisto");
combobox.setImmediate(true);
addComponent(combobox);
fieldGroup.bind(combobox,keys.getName() );

sorry i forgot something:


item = new PropertysetItem();
fieldGroup = new FieldGroup(item);
.
.


ComboBox combobox = new ComboBox("combi");
combobox.addItems("Io", "Europa", "Ganymedes", "Callisto");
combobox.setImmediate(true);
/* Adds the field to my extendet FormLayout */
addComponent(combobox);

/* We must set a Property 'combi' for the combobox */
item.addItemProperty("combi", new ObjectProperty<String>(""));

/* this binds the combobox to the name 'combi' */
fieldGroup.bind(combobox,"combi" );

You can get the container of all the Items that you have. Then as they are RowItems (if not you can use BeanItems) you have to create a custom Converter (RowIdToInteger) and than you can rappresent into combox the values of a specific property of the item

ComboBox combobox = new ComboBox();
combobox.setContainerDataSource(container);
combobox.setItemCaptionPropertyId(“TITLE”); // the item’s property that you want to view into combobox
combobox.setConverter(new RowIdToIntegerConverter());
combobox.setCaption(“Title”);