andreas
andreas

Reputation: 1595

Vaadin databinding between ListSelect and java.util.List<String>

I am new to vaadin and have a databinding problem. I have posted allready in the vaadin forum, but no answer up to now. if you answer here, I will of course reward it anyway.

https://vaadin.com/forum/-/message_boards/view_message/1057226

thanks in advance. greets, Andreas

Additional information: I tried allready to iterate over the items in the container, after pressing a save button. After deleting all original elements in the model collection, and adding copies from the container, the GUI breaks. Some other GUI elements do not respond anymore.

Upvotes: 1

Views: 7817

Answers (2)

miq
miq

Reputation: 2766

I have personally never used ListSelect, but I found this from the API docs:

This is a simple list select without, for instance, support for new items, lazyloading, and other advanced features.

I'd recommend BeanItemContainer. You can use it like this:

// Create a list of Strings
List<String> strings = new ArrayList<String>();
strings.add("Hello");

// Create a BeanItemContainer and include strings list
final BeanItemContainer<String> container = new BeanItemContainer<String>(strings);
container.addBean("World");

// Create a ListSelect and make BeanItemContainer its data container
ListSelect select = new ListSelect("", container);

// Create a button that adds "!" to the list
Button button = new Button("Add to list", new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        container.addBean("!");
    }
}

// Add the components to a layout
myLayout.addComponent(button);
myLayout.addComponent(select);

The downside (or benefit, it depends :) of this is that you can't add duplicate entries to a BeanItemContainer. In the example above the exclamation mark gets only added once.

You can get a Collection of Strings by calling:

Collection<String> strings = container.getItemIds();

If you need to support duplicate entries, take a look at IndexedContainer. With IndexedContainer you can add a String property by calling myIndexedContainer.addContainerProperty("caption", String.class, ""); and give each Item a unique itemId (or let the container generate the id's automatically).

Upvotes: 3

Marthin
Marthin

Reputation: 6543

Im not sure I understand your problem but I belive that it might be that you haven't told the controller to repaint. You do this be setting the datasource like this after the save event has occured.

listSelect.setContainerDataSource(listSelect.getContainerDataSource());

Upvotes: 1

Related Questions