Set a caption for the empty selection in ComboBox

I have a ComboBox with “setNullSelectionAllowed(true)”, so it shows an empty line at the top.
Is there any way I can edit the null row and replace it with some string? I would display something like “all elements”.

I’ve already tried setting “setNullSelectionAllowed(false)” adding a dummy string with “.addItem(new String(“all elements”))”, but since my ComboBox has a sqlContainer as data source, I get an error saying I can’t mix the sql entries with a String of my own. Hence I thought the easiest way (if possible) would be to just edit the text in the empty row.

One slightly different approach would be to use “setNullSelectionAllowed(false)” and then set the input prompt for the combo box i.e. “setInputPrompt(“Select item…”)”. In this way you will not have an empty line on top.

Hope this helps,
Goran

Hi,

did you try:

ComboBox box = new ComboBox(); box.setNullSelectionAllowed(true); box.setNullSelectionItemId("itemIdWhichRepresentsNull"); regards

Johannes

@Goran:
I already use setInputPrompt to indicate the box function to the user.
I basically need a “select all” function, so if I use “setNullSelectionAllowed(false)” I don’t have any methods left to perform the “select all”: if a user clicks on an option there’s no way for him to return to the null selection (wich as of now corresponds to the “select all”).

@Johannes:
I don’t even know how to get an Item Id which represents null. Since my combobox is filled directly from an SQL table, I think that would mean to insert an element which represents null directly in the DB, and that’s something I definitely can’t do.

I think the best thing to do -if possible- is just to style the empty line on top to display the “select all message”.

By style you mean using css? If so you can set an ItemStyleGenerator and add a class to your theme.

combobox.setItemStyleGenerator((source, itemId) -> (itemId == null) ? "null-selection" : null); .v-filterselect-item-null-selection span:before { content: 'No elements'; } Not the best solution but should work.
If you need to translate the label I think you can add a lang suffix, like this

.v-filterselect-item-null-selection-en span:before { content: 'All elements'; } .v-filterselect-item-null-selection-it span:before { content: 'Tutti gli elementi'; } HTH
Marco

Actually I used the word “style” improperly, but your solution still is intresting and may work as well. The only problem is I’m running my project with the 1.6 JRE, so I can’t use lambda expressions.
Moreover, Vaadin says that “the method setItemStyleGenerator is undefined for the type ComboBox”. I have Vaadin 7.4.0.

Hi,

you can use setItemStyleGenerator without Java8 syntax (just create an anonymous class for example), but unfortunately the
ItemStyleGenerator is only available since Vaadin 7.5.6
.

-Olli