Combo box items with additional properties

Hello!

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));

			item.getItemProperty(RESOURCE_ID).setValue(worker.getId());
		}
		
		
		controlSubjectComboBox.addListener(new Property.ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				controlSubjectComboBoxSelectionChanged(event);				
			}
		});

Then, when the user selects a certain item, the method
controlSubjectComboBoxSelectionChanged
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 -
event.getProperty().getValue()
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

Dmitri

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.

Hello!

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
toString
result) ?

Thanks

Dmitri

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.

Thanks!