Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
ComboBox different text in the title and in the items list
Dear friends,
I want to setup a ComboBox component that contains a list of companies. But in the title, I want to show just the company name and in the list of the companies, I want to provide some additional hints like City. Is this possible in ComboBox?
To illustrate this:
Company Ltd. <>
--------------------------------
| Company Ltd (London) |
| Company 2 Ltd (Paris) |
--------------------------------
Martin
You can use a GeneratedPropertyContainer and then set itemCaptionPropertyId in combobox
BeanItemContainer<Company> container = ...
GeneratedPropertyContainer gp = new GeneratedPropertyContainer(container);
gp.addGeneratedProperty("captionProperty", new PropertyValueGenerator<String>() {
@Override
public String getValue(Item item, Object itemId, Object propertyId) {
return buildCaption( (Company)itemId );
}
private String buildCaption(Company c) {
return String.format("%s (%s)", c.getName(), c.getCity());
}
@Override
public Class<String> getType() { return String.class; }
});
ComboBox comboBox = new ComboBox("", gp);
comboBox.setItemCaptionPropertyId("captionProperty");
Thank you, I was able to figure out how to display a custom property. But the problem is that it shows the same text in both text field and drop down list - I want to show a different text in the text field and different in the drop down list (as hints hints). This is probably not possible in Vaadin, but I'm rather asking whether there is some way how to do it :)
Sorry, completely misunderstood your question.
A very dirty way to reach your goal could be to have a custom ComboBox connected to a VFilterSelect extension with a custom TextBox; in TextBox.setText method you can modify the caption shown for selected item.
Not really a good solution by the way ;)
Server side
public class MyComboBox extends ComboBox {
// add constructors
}
Client side
public class MyFilterSelect extends VFilterSelect {
@Override protected TextBox createTextBox() {
return new FilterSelectTextBox() {
@Override public void setText(String text) {
String caption = text;
if (caption != null) {
caption = caption.substring(0, caption.indexOf('(')).trim();
}
super.setText(caption);
}
};
}
}
@Connect(MyComboBox.class)
public class MyComboBoxConnector extends ComboBoxConnector {
@Override public MyFilterSelect getWidget() {
return (MyFilterSelect)super.getWidget();
}
}
HTH
Marco