Multiple Default Values in ListSelect

Hello guys,

first of all, thank you for creating such a great framework.

I have a problem with setting the default values in ListSelect. Below you will my modified code from the Vaadin 6.4 demo ListSelectMultipleExample.java

Basically I want to set a couple of default values in ListSelect. It works if I only set only one default value, but not for more than one.

So my questions:
Is it even possible to set more than one default values in ListSelect? If yes, can someone give me a hint how to achieve that?

Thank you,

Christian


public class ListSelectMultipleExample extends VerticalLayout
    implements Property.ValueChangeListener {

    private static final String[] cities =
        new String[] { "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo",
            "Paris", "Stockholm" };

    public ListSelectMultipleExample() {
        setSpacing(true);

        final ListSelect l = new ListSelect("Please select some cities");

        for (final String city : cities) {
            l.addItem(city);
            final Set<String> selected = new HashSet<String>();

        }
        l.setRows(7);
        l.setNullSelectionAllowed(true);
        l.setMultiSelect(true);
        l.setImmediate(true);
        l.addListener(this);

        final Set<String> selected = new HashSet<String>();
        selected.add("Berlin");
        selected.add("Helsinki");

        l.select(selected);
        addComponent(l);

    }

    /*
     * Shows a notification when a selection is made.
     */
    public void valueChange(final ValueChangeEvent event) {
        getWindow().showNotification("Selected cities: " + event.getProperty());

    }
}

If I remember correctly you should either do like that and

l.setValue(selected);

or

l.select("Berlin");
l.select("Helsinki");

Hello Arthur,

thank you for your time and answer.

I tried it using select(), and that solves the problem.

Thanks and regards,

Christian

Hi,

I have the same problem, i’m trying select a default values in a listselect which is in a form.


                 Iterator it=publication.getAutors().iterator();
		final Set<Integer> selected = new HashSet<Integer>();
		while (it.hasNext()){ 
			Author author=(Author) it.next();
			selected.add(author.getId());
		}
		
		ListSelect list=(ListSelect)getField("idauthor");
		list.select(selected);

But do no work…!!
Any idea that what is wrong?

The select() method takes just one itemId at a time, so you could put it in your while-loop there and select the items one by one.

If you use a collection to set the selected values, you have to use setValue(), which takes a collection in multiselect mode.

Hi, thank you…!!



I'trying with the two options 

		Iterator it=publication.getAutors().iterator();
		final Set<Integer> selected = new HashSet<Integer>();
		while (it.hasNext()){ 
			Author author=(Author) it.next();
			selected.add(author.getId());

		}
		
		ListSelect list=(ListSelect)getField("idauthor");
		list.setValue(selected)

And continues not working…!!

I don’t know what is happening!! Any idea that what could be the problem?

How did you create your ListSelect?

How did you populate the possible values?

How did you specify the property type for the values?

It seems since you are directly setting the select’s value (hashset), you need to be sure you set the ListSelect as setMultiSelect(true) and then you need to have built the select’s possible values correctly using Integer values in your case. And then the property that represents your Integer field id needed to be specified as type java.util.Set.class.

It doesn’t seem like you are using an Item to set the values in your Form’s ListSelect, so I’m not sure how to do it all directly. The key is that you somehow need to tell the ListSelect that the values are a Set type (with beans, the get/set methods just use Set, and with Items, you need to addItemProperty specifying the type of Set like above). Otherwise, I’m not sure how to do it.

this is the creation on PublicationFieldFactory that extends from DefaultFieldFactory

In class POJO

	public java.util.Set<Integer> idauthor;

....
and getteres and setters....


final ListSelect authorslist = new ListSelect()
		

...constructor....

authorslist.setContainerDataSource(NaturamanagerApplication.storage.getAuthorsFullNameIndexedContainer(null));
		authorslist.setItemCaptionPropertyId(DatabaseStorage.FULLNAME_PROPERTY);
		authorslist.setRows(6);
		authorslist.setWidth("100%");
		authorslist.setImmediate(true);
		authorslist.setMultiSelect(true);
		authorslist.setRequired(true);
		authorslist.setInvalidAllowed(false);
		authorslist.setNullSelectionAllowed(false);

 

And this is the container

	public IndexedContainer getAuthorsFullNameIndexedContainer(Integer idpublication) {
		Collection<Author> collection = getAuthorsCollection(idpublication);
		IndexedContainer ic=new IndexedContainer();
		ic.addContainerProperty("fullname", String.class, null);
		Iterator<Author> it =collection.iterator();
		Item itemAuthor;
		while (it.hasNext()){
			Author author=it.next();
			itemAuthor=ic.addItem(author.getId());
			itemAuthor.getItemProperty("fullname").setValue(author.getLastname() + ", " +  author.getFirstname());
		}
		return ic;
	}

And do no work the selection of default values…:frowning: