Problem with ComboBox

Hi to everyone,

I’ve been reading about Vaadin for the last few months.
I want to bind a table named PARTS with fields ID, PART to a combobox as datasource. The idea is to show the PART field as caption and get the ID field when selected to store it into a nested table, let’s say CARS.
By now the container of PARTS is indexed.

At last, whatever I do, it keeps retrieving the index order and not the ID property of the item.

could anyone help me?

Thank you all.

To display the “PART” property (field/column/whatever) as the visible item caption in the ComboBox list, you need to set:

select.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
select.setItemCaptionPropertyId("PART");

When you select an item, you can get the
itemId
of the selected item with [tt]
select.getValue()
[/tt]. Then you can use [tt]
select.getItem(itemId)
[/tt] to get the item. You can then get the “ID” property with [tt]
getItemProperty()
[/tt], for example.

For example, if the ID field (property) is an integer, you could say in a value change listener:

// Get the selected item
Object itemId = event.getProperty().getValue(); // or select.getValue()

// Get the property/field/column value
Integer idValue = (Integer) select.getItem(itemId).getItemProperty("ID").getValue();

// Do something with it
getWindow().showNotification("Clicked planet #" + idValue.toString());

See the example on
property caption mode
with a bean container.


Edit
1/10/11: Corrected the example link.

Thank you for your reply,

I think this fits when you have your SELECT component into a standard gui with listeners and all that stuff as it is showed in example you suggest, but if I use it in a form like this:

form.setFormFieldFactory(new CarsFieldFactory());
form.setItemDataSource(carPojo);

where, one of the object of the FormFieldFactory is this combobox:

combo.setContainerDataSource(new PartsDataSource());
combo.setItemCaptionPropertyId("PART");
combo.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);

showing the PART name,

but when I do the

form.commit();

I do not have the chance to handle the return value from the combobox (what you suggest) before the form gets it into the carPojo and, at last, the carPojo stores the order the PART was in the container and not the ID the PART has.
I think here would be the right place to put the second block of code you gave me but I do have a clue how to do it.

Furthermore, now I am thinking, I don’t have even the chance to set the combobox, prior of showing it in the form, with the right value…(in your example, setting up the combobox showing the planet with ID=3, would show “Mars” instead of “Earth” ).

thanks in advanced…

Hi,

have you solved the problem? I have the same.

greetings,
Thomas

You might want to look at how the “station” property is handled in the
FillEditor.java
example (see also the
on-line demo
. In that case, the selection made with a ComboBox is bound to a form property, which is written in commit().

Does that help?

thanks, I’ll look at that and get back again.

Hi again,

my problem is a little different. I think it has nothing to do with the container.
Please look at my new Post:
link

Thanks,
Thomas