Listening to a Table with BeanItemContainer

Hi,

I’ve usually listened to selects on my Tables with a Table.ValueChangeListener. However, using BeanItemContainers I don’t seem to get the events. There appears to be a
ticket
about this.

Is there any way to enable single-selection for such a Table?

Hi!

I think using BeanItemContainer, Table and ValueChangeListeners should work just fine together. We do it for example in our AddressBookApplication. The valuechange event is fired by table, so selection does not depend on backing container at all.

Ensure your Table is set selectable and immediate to use it properly as a select. Below is a tiny example using the Person bean from AddressBookApplication


        final Window main = new Window();
        setMainWindow(main);

        BeanItemContainer<Person> container = new BeanItemContainer<Person>(
                Person.class);

        Person p;
        p = new Person();
        p.setFirstName("Me");
        container.addBean(p);
        p = new Person();
        p.setFirstName("Myself");
        container.addBean(p);
        p = new Person();
        p.setFirstName("I");
        container.addBean(p);

        final Table t = new Table();
        t.setContainerDataSource(container);
        t.setImmediate(true);
        t.setSelectable(true);

        main.addComponent(t);

        t.addListener(new Table.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                main.showNotification("Selected "
                        + ((Person) t.getValue()).getFirstName());
            }
        });

cheers,
matti

Thanks for your help! The setSelectable and setImmediate were were missing and that was causing the trouble.