Combobox select/setValue

I have a completely simple case that doesnt work. I want to create a ComboBox and set a default/initial value.
The creation works fine, setting the initialValue programatically just will not work.

I wrapped the caption and value in a class.


public class SelectItem<T> {

	String caption;
	T value;
.. getter and setter, generated equals and hashCode methods

Now I want to create the ComboBox with a list of my SelectItems and an index into that list.


	private ComboBox createComboboxFromSelectItemList(String caption) {
		ComboBox combo = new ComboBox(caption);
		for (SelectItem<?> item : modelList) {
			combo.addItem(item.getValue());
			combo.setItemCaption(item.getValue(), item.getCaption());
		}
		combo.setItemCaptionMode(Select.ITEM_CAPTION_MODE_EXPLICIT);
		combo.setValue(modelList.get(defaultModelIndex).getValue());
		combo.requestRepaint();
		return combo;
	}

I tracked it down to setInternalValue(newValue); in AbstractField which it does set. Why it doesnt get selected in the Combobox remains a mystery to me.
What did i do wrong?
Did i miss something?

Hm. I just tested your code in a very simple case and it works for me (I’m using Vaadin 6.7.2 in case it makes a difference). The only thing that comes to mind from those lines of code is that your repaint request won’t help you where it is since the ComboBox hasn’t been attached yet, but that shouldn’t have any bearing on your problem. Are you using combo.setImmediate(true) somewhere we can’t see it? If not, the value won’t update before you do something else that triggers a server round-trip. It will happen eventually, but it might be too late if you’ve already tried to use that value for something by then. But if that’s not the problem, perhaps you could extend the example a bit to give us a better idea what you are doing?

I thinks your equals/hashCode method do not work and that’s why your comboBox cannot set the value.

I just copy/paste your code and then created your Class SeclectedItem. Eclipse generated getter/setter/equals and hasCode.

The result… it’s work verry well.

Here the code for SelectedItem


public class SelectItem<T> {
	
	 String caption;
	 T value;
	 
	 
	public SelectItem(String caption, T value) {
		super();
		this.caption = caption;
		this.value = value;
	}
	public String getCaption() {
		return caption;
	}
	public void setCaption(String caption) {
		this.caption = caption;
	}
	public T getValue() {
		return value;
	}
	public void setValue(T value) {
		this.value = value;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((caption == null) ? 0 : caption.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SelectItem other = (SelectItem) obj;
		if (caption == null) {
			if (other.caption != null)
				return false;
		} else if (!caption.equals(other.caption))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}
	 
	 

}

Thanks alot anna and eric for your time and efforts. Unfortunately i couldnt find the error so far. Its good to know that it is working in normal cases. Knowing that might help. Thx.
I also use Vaadin 4.7.2. My case is a bit more complicated, its hard to decide which part to cut and present to you to shed more light on it without confusing you. I actually have a custom fieldfactory, some mappinginformations, fielddefinitions and so forth.
My equals and hashCode methods i also did generate with eclipse. actually they shouldnt be neccessary because i dont put SelectedItems into the combobox, but the attributes of SelectedItem (value and caption).
I will try a bit more, before doing some big refactoring. And then i maybe pluck some newly grown gray hairs. :slight_smile:

Back again. So after some thoughts about my use case being not so simple i figured that the reason might be the nesting of my beans and properties. And it is.
To make things short, when creating a field for a nested item in a FieldFactory u have to set the PropertyDataSource by yourself. I spare the details, the createComboBox-Method had to look like this.


	@SuppressWarnings("unchecked")
	private ComboBox createComboboxFromSelectItemList(String caption, Object fieldDataSource) {
		ComboBox combo = new ComboBox(caption);
[b]
		combo.setPropertyDataSource((Property)fieldDataSource);
[/b]
		List<SelectItem<?>> modelList = (List<SelectItem<?>>) modelData;
		for (SelectItem<?> item : modelList) {
			combo.addItem(item.getValue());
			combo.setItemCaption(item.getValue(), item.getCaption());
		}
		combo.setItemCaptionMode(Select.ITEM_CAPTION_MODE_EXPLICIT);
		combo.setValue(modelList.get(defaultModelIndex).getValue());
		return combo;
	}

thx again, letting me know that the former version was working, motivated me enough to take another look.

Hi, Guys!
As a last resort I call for help in here.
I have got a Collection of Dto’s shown in a datagrid.
In order to edit one record (selectedEmailvorlageItem) I open up a window showing a form.
The formfields are generated with the EmailvorlageFieldFactory().

	private class EmailvorlageFieldFactory extends DefaultFieldFactory {
		public EmailvorlageFieldFactory() {
		}

		@Override
		public Field createField(Item item, Object propertyId,
				Component uiContext) {
			Field f;
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
			if ("mandant".equals(propertyId)) {
				BeanItemContainer<MandantDto> mandantenContainer = new BeanItemContainer<MandantDto>(
						MandantDto.class);
				mandantenContainer.addAll(AppModel.getMandantenList());
				ComboBox cbMandant = new ComboBox("Mandant", mandantenContainer);
				cbMandant.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
				cbMandant.setItemCaptionPropertyId("bezeichnung");
				cbMandant.setImmediate(true);
				return cbMandant;
			} else if ("bezeichnung".equals(propertyId)) {
				TextField tf = (TextField) f;
				tf.setRequired(true);
				tf.setRequiredError("Das ist ein Pflichtfeld!");
				tf.setWidth(COMMON_FIELD_WIDTH);
				tf.addValidator(new StringLengthValidator(
						"Feldlänge zwischen 1-100 Zeichen!", 1, 100, false));
			} else if ("beschreibung".equals(propertyId)) {
				TextField tf = (TextField) f;
				tf.setRequired(true);
				tf.setRequiredError("Das ist ein Pflichtfeld!");
				tf.setWidth(COMMON_FIELD_WIDTH);
				tf.addValidator(new StringLengthValidator(
						"Feldlänge zwischen 1-100 Zeichen!", 1, 100, false));
			}
			return f;
		}
	}

As you can see, I show the list of ‘mandant’ in a ComboBox, which works fine.
But in Edit-Mode I want to have the correct value selected in the ComboBox.

This is how the form is created.

		BeanItem<EmailvorlageDto> emailvorlageItem = new BeanItem<EmailvorlageDto>(
				selectedEmailvorlageItem);
		// Create the Form
		VerticalLayout vForm = new VerticalLayout();
		vForm.setMargin(true);
		final Form emailvorlageForm = new Form();
		emailvorlageForm.setWriteThrough(false);
		emailvorlageForm.setInvalidCommitted(false);
		// FieldFactory for customizing the fields and adding validators
		emailvorlageForm.setFormFieldFactory(new EmailvorlageFieldFactory());
		emailvorlageForm.setItemDataSource(emailvorlageItem); // bind to POJO
		//initialwerte setzen
		ComboBox cb = (ComboBox)emailvorlageForm.getField("mandant");
		Integer mandantPk = (Integer)cb.getValue();
		System.out.println("selektierter Mandant-PK: " + mandantPk);

I can see the correct value is selected when I get the ComboBox-value from the form,
but i cannot see the correct value selected on the screen.
After 2 days of struggling around I’m a little desparate now.

Can anybody help me out?

best regars from styria/austria
Wolfi

Anyone know the solution for the problem presented by Wolfgang Poelzl ?

Hi.

I run into the same problem. I create the ComboBox for the nested Property (the department of a person) via the FormFieldFactory and all the values are available, if I open the combo. But the current value is not displayed. But if I debug log the current value, it is there. So it seems to be a display problem, but I don’t know how to solve. I tried setImmidiate(), repaints and even visible off/on, but nothing helps.

I also found many other threads with the same problem. So perhaps someone from Vaadin could take a look at this seemingly casual use case, which runs into problems for many users.

Thanks a lot and bye
Horst

I suspect that Hibernate etc. give you different instances of the nested bean than those on the list, and the IDs do not have suitable equals()/hashCode() methods.

Hi Henri.

Thanks for your quick reply. equals/hasCode was missing! Thanks a lot for your help!!!

Bye
Horst