Default item in a com.itmill.toolkit.ui.Select - How-to?

Hi,

How do I set an item as the default item in a Select component - I want the item to be visible when the Select component is first rendered on screen and if no other item is later on selected by the user that one (the default) should be sent to the server.

Thank you for your attention,

Johnny

Use the .select() function.

Code from the select example in the documentation:


        Select mySelectField = new Select("Select something here");
        main.addComponent(mySelectField);
        /* Fill the component with some items. */
        final String[] planets = new String[]
 { "Mercury", "Venus", "Earth",
                "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
        for (int i = 0; i < planets.length; i++)
            mySelectField.addItem(planets[i]
);

        mySelectField.select("Saturn");

Hopes it helps.

Tried it to no avail.

The Select is in a Form. Does that change anything?

Regards,

Johnny

That’s weird, I wrote a little app to test that, and the code I gave was a direct copy/paste, which worked.

Are the items just a single String or are they items that have a different ID that identifies the options?

A code snippet of how you initialize/populate your select field would be nice!

Here’s the code:


        @Override
        public Field createField(final Item item, final Object propertyId,
                final Component uiContext) {

            final String pid = (String) propertyId;

            if (pid.equals("test")) {

                Object defaultItemID = 2;

                final TreeMap<Object, String> items =
                        new TreeMap<Object, String>();

                items.put(defaultItemID, "ALL");
                items.put(1, "YES");
                items.put(0, "NO");

                return createSelectField("On Test", items, defaultItemID);

            }

            return null;
        }

        private Select createSelectField(final String caption,
                final Map<Object, String> items,
                final Object defaultItemID) {

            final Select select = new Select(caption);

            for(Map.Entry<Object, String> e : items.entrySet()) {

                select.addItem(e.getKey());  
                select.setItemCaption(e.getKey(), e.getValue());

            }

            select.select(defaultItemID);

            return select;

        }

I tried your code and it always selected the one item that I gave the defaultItemID. There has to be something with the context, as this seems to work. Hard to say when I’m not familiar with the project as a whole. Here’s the full file that I used. The id’s have been changed as I tested a few options, but no bugs were found.


package helloWorld;

import java.util.Map;
import java.util.TreeMap;

import com.itmill.toolkit.Application;
import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.ui.Component;
import com.itmill.toolkit.ui.Field;
import com.itmill.toolkit.ui.Select;
import com.itmill.toolkit.ui.Window;

public class Hello extends Application {
    Window main;

    @Override
    public void init() {
        main = new Window("Hello world window");
        setMainWindow(main);
        Select select = (Select) createField(null, "test", null);
        main.addComponent(select);
        select.select("Saturn");
    }

    public Field createField(final Item item, final Object propertyId,
            final Component uiContext) {
        final String pid = (String) propertyId;
        if (pid.equals("test")) {
            Object defaultItemID = 5;
            final TreeMap<Object, String> items = new TreeMap<Object, String>();
            items.put(8, "ALL");
            items.put(3, "YES");
            items.put(defaultItemID, "NO");
            return createSelectField("On Test", items, defaultItemID);
        }
        return null;
    }

    private Select createSelectField(final String caption,
            final Map<Object, String> items, final Object defaultItemID) {
        final Select select = new Select(caption);
        for (Map.Entry<Object, String> e : items.entrySet()) {
            select.addItem(e.getKey());
            select.setItemCaption(e.getKey(), e.getValue());
        }
        select.select(defaultItemID);
        return select;
    }
}

It’s working now! :smiley:

As I posted before, the Select is in a Form. I set the Form’s data source like this


    private final Criteria criteria = new Criteria();
    (...)
    form.setItemDataSource(new BeanItem(criteria));

and this is the code for Criteria


    public class Criteria {

        private Object test = 2; // THIS WAS NULL BEFORE

        public Object getTest() {
            return test;
        }

        public void setTest(final Object test) {
            this.test = test;
        }

    }

The Select component is getting its value from Criteria.test. So now I changed the previously posted code to


    public class FieldFactory implements com.itmill.toolkit.ui.FieldFactory {

        (...)

        @Override
        public Field createField(final Item item, final Object propertyId,
                final Component uiContext) {

            final String pid = (String) propertyId;

            if (pid.equals("test")) {

                final TreeMap<Object, String> items =
                        new TreeMap<Object, String>();

                items.put(2, "ALL");
                items.put(1, "YES");
                items.put(0, "NO");

                criteria.setTest(2);    // set the default item ID

                return createSelectField("On Test", items);

            }

            return null;
        }

        (...)

        private Select createSelectField(final String caption,
                final Map<Object, String> items) {

            final Select select = new Select(caption);

            for(Map.Entry<Object, String> e : items.entrySet()) {

                select.addItem(e.getKey());  
                select.setItemCaption(e.getKey(), e.getValue());

            }

            return select;

        }

    }

and ‘ALL’ is now the visible item when the Select is first rendered on screen - making it the default item.

Best regards,

Johnny

nice! :slight_smile: