'Vaadin 8 ComboBox set items as html not work
the code:
ComboBox<String> comboBox = new ComboBox<>("TEST-Combo");
comboBox.setCaptionAsHtml(true);
comboBox.setItemCaptionGenerator(item -> "<b>" + item + "</b>");
comboBox.setTextInputAllowed(false);
comboBox.setItems("xxx", "<i>yyy</i>", "<b>zzz</b>");
reuslt:
Is it Vaadin Bug or my failure?
EDIT
Vaadin Version 8.5.2
Solution 1:[1]
Method
comboBox.setCaptionAsHtml(true) does not affect items. It sets the mode of the ComboBox caption, which in your case is "TEST-Combo".
There is currently no HTML support for items in ComboBox in Vaadin 8.
However there is comboBox.setStyleGenerator() which allows you to set item specific styles in CSS. I.e. if you case is to set bold font, you can set something like
comboBox.setStyleGenerator(item -> item.isImportant() ? "bold-font" : "");
and in theme SCSS mixin
.bold-font {
font-weight: bold;
}
Solution 2:[2]
It's a missing feature. To help prevent XSS vulnerabilities, the item captions are rendered as plain text instead of HTML. There is a ticket about making it possible to configure it to show the contents as HTML, but it has been inactive for a long time so it's not very likely that it would be fixed any time soon.
Solution 3:[3]
You can use a custom renderer (an HtmlRenderer in your case):
ComboBox<String> comboBox = new ComboBox<>();
comboBox.setRenderer( new HtmlRenderer() );
This makes your combo items being rendered in HTML
A more sophisticated renderer is explained here: https://vaadin.com/components/vaadin-combo-box/java-examples/using-components
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ollitietavainen |
| Solution 2 | Leif Åstrand |
| Solution 3 | TacheDeChoco |

