Read values of compontens in formlayout?

Hey,

i create dynamically some html-compontens, like checkbox or radio buttons:

final FormLayout formLayout = new FormLayout();
for(int a=0; a<someContainer.size(); a++) {
      optionGroup = new OptionGroup();
      optionGroup.setMultiSelect(false);
      optionGroup.setNullSelectionAllowed(false);                        
      optionGroup.addItem(someContainer.getIdByIndex(a).getCaption());
}
formLayout.addComponent(optionGroup);

after this, i add a button. with this button, i want to get the selected values of all compontents separately.

button = new Button("Get Values", new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
       try {
            for(int a=0; a<formLayout.getComponentCount(); a++) {
                  System.out.println(formLayout.getComponent(a).toString());
            }
      } catch (Exception e) {
      }
}
});                
formLayout.addComponent(button);

So, my first problem:
I get only a value, when i use .toString(). But i get the following waring: “You are using toString() instead of getValue() to get the value for a Property of type com.vaadin.ui.OptionGroup”. But a function like getValue() is not existing!?

My second problem:
How can i add a label to FormLayout without to set it as component?
With “Form” you can do it like this: form.getLayout().addComponent(label);
How it works with FormLayout? Because a label is also listing in getComponent(a).

Thanks for your help!

First issue: you are using the Component interface, that doesn’t have getValue(). Cast the component to a Field.

Second issue: the FormLayout is exactly like any other layout; you can add components to it, that’s it. It doesn’t differentiate between Labels and Fields. Calling addComponent will add the Label, and it will show up when calling getComponent. You can check the type of the component with e.g. ‘instanceof Field’ in your loop, but what I would do is use a FieldGroup instead. The FieldGroup is what replaced the Form from Vaadin 6.

The
getValue()
method is defined in the interface
Field
that is a subtype of
Component
. You could either keep track of your fields in a separate list, like
List
, or, when you loop through the layout containing the fields, check and cast accordingly:

for(Component c : myLayout) {
    if(c instanceof Field) {
        Field f = (Field) c;
        doSomethingWith(f.getValue());
    }
}

Thanks, it works.
But i have a problems with multiple optiongroups (selects).
A multiple optiongroups returns normally an array, but with getValue(), i get a string in this format [option1, option2, option3]
.

But i need it as an array. How can i convert it to a array?

Thanks.

getValue() doesn’t return a String but a Set for multi-select fields - probably you are treating/printing it so that Java calls toString() implicitly. You can use any standard Java approach to convert it to an array if you really need one.

Thx, but i dont understand.
what should i do, when i want to get a array or a list of multiple option groups, with this code:

String arrString;
for(int a=0; a<formLayout.getComponentCount(); a++) {
    arrString = ((Field<?>)formLayout.getComponent(a)).getValue();  /// ???
}

thank you.

getValue returns a Set, not a String (when in multiselection mode). You can then create an array from the Set using any number of standard Java techniques.

Trust me, it’s not a String, even if it looks like it. You can use a debugger to check it for yourself.