Table + BeanItemContainer components interaction

Hello,
I’m currently using a CustomFieldFactory in conjuction with a BeanItemContainer and a Vaadin table since I need 2 ComboBoxs for 2 different columns for each row of the table.
However I need 2 of those ComboBoxes to interact on value Change; I found this example in the book :


http://demo.vaadin.com/book-examples/book/#component.table.components.interactingcomponents

Which does exactly what I need.
However in my case since I’m using a BeanItemContainer whe I call :

ComboBox combo = ((ComboBox )table.getContainerProperty (itemId, "subtype").getValue()); I get an ObjectProperty instead of the ComboBox.
Is there a way to accomplish this behavoir using a BeanItemContainer?

Thank-you in advance

Ulises

Hello Ulises,

What kind of interaction are you looking to build with the ComboBoxes? There is no simple way to get a reference to the actual ComboBox, as the container is not aware of the component the table is displaying. However, since you’re using a BeanItemContainer, using:

table.getContainerProperty (itemId, "subtype").getValue(); returns the selected value of the ComboBox, as the table binds the ComboBox to the propertyId (and to the actual field in your bean) you’ve defined in your FieldFactory.

If this is not enough and you want the behaviour to mimic the book example, one possible way is to use components inside the bean, as described in the next example in the book:

http://demo.vaadin.com/book-examples/book/#component.table.components.beancomponents

Hi,
Thanks for your answer.
Let’s say I have a row in my table with 2 ComboBoxes (1 in column 2, the other one in column 3) lets call them cb1 and cb2
The behaviour I’m trying to achieve is the following :

  1. User changes the value of the cb1 .
  2. When the ValueChangeListener callback is triggered I need to recover the currently selected value cb1 and update the values for cb2 with a SQL Query that filters the items based on the value of cb1.

If I understand corretly from your answer I cannot get a hold of the ComboBox (cb2) from my ValueChangeListener on cb1 without creating a BeanComponent; is this correct?

Thanks again

Ulises

Yes you understood correctly, unless you keep track of the Comboboxes manually. However, you can take another approach to solving your problem by having the 2nd ComboBox listen to changes in the Property that the 1st ComboBox is bound to.

A quick example, assuming that the first ComboBox is bound to propertyId “property1”:

// in the FieldFactory, when creating the 2nd ComboBox:

final ComboBox combo2 = new ComboBox();

Property.ValueChangeNotifier p =
    (Property.ValueChangeNotifier)table.getContainerProperty(itemId, "property1");
p.addValueChangeListener(new ValueChangeListener(){
    @Override
    public void valueChange(ValueChangeEvent event) {
        Object value = table.getContainerProperty(itemId, "property1").getValue();
        combo2.removeAllItems();
        // populate combo2 based on value..
        combo2.addItems(...);
    }
});

return combo2;

Hi again,
This works perfectly!
Thank-you very much for your fast answer.

Ulises