Enable/Disable field during setItemDataSource doesn't work

Hey,
I have some fields, that should be disabled, for some values from another combobox. So I’ve written a value change listener:

        cdcpTypeField.addValueChangeListener(event -> {
            SecurityType value = (SecurityType) event.getProperty().getValue();
            if (C1.equals(value )
                || C2.equals(value)
                || C3.equals(value)
                || C4.equals(value)){
            enableFieldGroup(true);
        }else{
            enableFieldGroup(false);
        }});

This works fine. when I change the combobox value manualy, but doesn’t work, when the value is changed with
setItemDataSource.
Value change listener is executed properly (I debugged it), but the fields are still enabled. How can I achieve disabling of the fields, when the window is opened with initial value?

Agata

I think the datasource is assigned to the fieldGroup prior to Value Change event being registered to the combobox.
You have to do something like this.

Combobox comboBox = new ComboBox(); addValueChangeListener(comboBox); setItemDataSource(someDataSource);
Note:
This is just the algo. not a working code.
If this what is done in your code, please share the code.

As I wrote above value change listener
is
fired, but the components
still are enabled.

Can you please share the snippet of on you are doing? I have done something like shown below and its working perfectly fine…

        ComboBox box = new ComboBox();
        IndexedContainer container = new IndexedContainer();
        box.setContainerDataSource(container);
        Item enable = container.addItem("Enable Fields");
        Item disable = container.addItem("disable Fields");
        box.setImmediate(true);
        box.setNullSelectionAllowed(false);
        box.addValueChangeListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {

                System.out.println(event.getProperty());
                if ("Enable Fields".equals(event.getProperty().getValue())) {
                    System.out.println("Enableing group");
                    group.setEnabled(true);
                } else {
                    System.out.println("Disableing group");
                    group.setEnabled(false);
                }
            }
        });
        box.setValue("Enable Fields");

It’s not easy to write it in few lines as the class is rather complex and in complex hierarhy. But in shortage:

public class MyWindow<T> extends Window{

    private final Class<T> beanType;
    protected final T dto;

    protected BeanFieldGroup<T> dtoFields;
   
  public MyWindow(Class<T> type, T dto){
    super("caption");
    buildMainLayout();
    this.beanType = type;
    this.dto = dto;
    dtoFields.setItemDataSource(dto)
  }

  protected void buildMainLayout() {
        .
        .
        cdcpTypeField = new ComboBox("Type",
                Arrays.asList(SecurityType.values()));
        cdcpTypeField.addValueChangeListener(event -> {
            SecurityType value = (SecurityType) event.getProperty().getValue();
            enableFieldGroup(value);
        });
        layout.addComponent(cdcpTypeField);
        dtoFields.bind(cdcpTypeField, "type");

        .
        .
  }

}

Can you please share the Bean that you are using to Bind to the fieldgroup?

Also, can you please check if the field type, which is of type enum SecurityType, is initialized with appropriate value. If not ensure that enum is initialized with some default value, say which disables the field group by default.

Thanks,
Krishna.

It is simple dto class with getters and setters and it is initialized properly: When I add following code to the end of constructor:

enableFieldGroup((SecurityType) cdcpTypeField.getValue()); the window (form) is initialized properly (group of fields is disabled).