Combo box items with additional properties


I have a combo box and want to fill it with items that have 2 properties -

a) the item caption and
b) the primary key of the record in the database.

When the user selects some item, I want to retrieve the primary key (b) and save it for further use.

I fill the combo box in this way:

		controlSubjectComboBox = new ComboBox();

		controlSubjectComboBox.addContainerProperty(RESOURCE_ID, Long.class, null);
		final List<Worker> workers = this.persistence.getAllWorkers();
		for (Worker worker : workers)
			final String template = TM.get("editingprocesspanel.14-resource-combobox-item");
			final String resourceText = template.
					replace("${abbreviation}", worker.getAbbreviation()).
					replace("${firstName}", worker.getFirstName()).
					replace("${middleName}", worker.getMiddleName()).
					replace("${surname}", worker.getSurname());
			final Item item = controlSubjectComboBox.addItem(StringUtils.abbreviate(resourceText, 50));

		controlSubjectComboBox.addListener(new Property.ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			public void valueChange(ValueChangeEvent event) {

Then, when the user selects a certain item, the method
is called:

	protected void controlSubjectComboBoxSelectionChanged(final ValueChangeEvent  event) {
		LOGGER.debug("event.getProperty(): " + event.getProperty());
		LOGGER.debug("event.getProperty().getClass(): " + event.getProperty().getClass());
		LOGGER.debug("event.getProperty().getValue(): " + event.getProperty().getValue());	

And here’s the problem -
returns the caption of the item.

How can I get the value of the property RESOURCE_ID of the currently selected item?

Thanks in advance


instead of

 final Item item = controlSubjectComboBox.addItem(StringUtils.abbreviate(resourceText, 50));

you could do

 final Item item = controlSubjectComboBox.addItem(worker);

which means that you can get the selected worker object from the combobox with

Worker worker = (Worker)controlSubjectComboBox.getValue();

and then get the id or any other item that you need with the getters (here worker.getId())

Alternatively, you could use your code, and fetch the Item-object with the caption you have, and from there fetch the id.

Something like this:

Item item = controlSubjectComboBox.getItem(event.getProperty.getValue());
Object value = item.getItemProperty(RESOURCE_ID).getValue()

But I still prefer to have the object itself as the item ID because then you can easily access any and all of its’ properties.


Thanks, this solved my problem!

One question - is it possible to tell the combo box, which of the worker’s properties should be used for caption (instead of
result) ?



No, you can’t put a property to the caption automatically, but you can say that a property in you container should be the caption. Make an other property, like your RESOURCE_ID, which contains the caption you want. Then you can just say controlSubjectComboBox .setItemCaptionPropertyId(theProperty) when you create the select, and it will use that property as caption.