Select: setting the selected value

hi

I have a Select and I want to set the value. The code is below. Its not giving any errors, the select is filled with the “Users” but the selection is not done. Its displayed as if nothing has been selected.

	Select<User> tildeltBruger;
    ArrayList<User> list;
	list = UserService.getAll();
    tildeltBruger.setItems(UserService.getAll());
    tildeltBruger.setItemLabelGenerator(User::getUsername);
    tildeltBruger.setValue(list.get(0)); // actually I want setValue(currentUser); but that also does not work so I tried just selecing first from list

What should be done different? If I use ComboBox this all works.

Vaadin flow 14.newset stable

Hi Jan,

It is very likely that your entities doesnt implement equals correctly. You are retriving a list of users twice. One of the lists is used to set the available users and the other to select a user. If equals is not correct, nothing will be selected as the select cant find the selected user in the list of items.

Try fetching the list once and use the list both to set the items and also to select the value. If that works, it is definitely equals().

Also you should call setItemLabelGenerator() before you set the items.

Hi Jan

Select has the great advantage over ComboBox in that it can have a Renderer that renders the options, and the selected option as well. I believe setting an ItemLabelGenerator only works for the non-selected options.

What this means is, you can have any Component being shown instead of just a text. You can still choose to show just text using a TextRenderer.

//tildeltBruger.setItemLabelGenerator(User::getUsername); // remove this line
tildeltBruger.setRenderer(new TextRenderer<>(User::getUsername));
// or shorthand method:
tildeltBruger.setTextRenderer(User::getUsername);

thanks

just to be clear, my code would normally look like this:

Select<User> tildeltBruger;
tildeltBruger.setItems(UserService.getAll());
tildeltBruger.setItemLabelGenerator(User::getUsername);
tildeltBruger.setValue(loginUser);

from a rendering point of view this is OK, works fine, shows the userlist correctly in the select, showing “username”, and selecting a user returns a User object and so on.

But I want to set the initial value, default select to the currently logged in user. This is the exact same user object from the samt list (getall - its static right now). And as noted, if I use the exact same code but with a Combobox replacing the Select, this all works. Also setting the default value - selecting a use in the list from servercode. And the combobox showes the selected value.
I will try looking at the Object.equals() method as suggested, but I would still think it must be a unwanted thing to have to vaadin components that work differently in .setValue like this? Why would setValue work ok without changing equals() but select does not?

Hi Jan,

ComboBox allows for an “unknown” item to be set as the value. That is needed to support custom values. Select does not offer that option. If you do setValue in Select with an object that wasnt set with setItems, it will be ignored.

ComboBox<String> comboBox = new ComboBox<>("Fruit");
comboBox.setItems("Apple", "Orange", "Banana");

/**
 * Allow users to enter a value which doesn't exist in the data set, and
 * set it as the value of the ComboBox.
 */
comboBox.addCustomValueSetListener(
        event -> comboBox.setValue(event.getDetail()));

comboBox.addValueChangeListener(event -> {
    if (event.getValue() == null) {
        message.setText("No fruit selected");
    } else {
        message.setText("Selected value: " + event.getValue());
    }
});