ComboBox not indicating default value again

Hi Folks,

I have an extremely annoying (e.g. “hit head on table repeatedly”) problem with ComboBox not indicating the default value I select.

Here is the code:


	ComboBox cb = new ComboBox("Country");
	cb.setRequired(true);
	cb.setRequiredError("Please select the Country");
	cb.setNullSelectionAllowed(false);
	
	boolean foundDefault = false;
	// getSet returns a Set<Country> of all Country's
	for (Country c : getSet(false)) {
	    cb.addItem(c);
	    cb.setItemCaption(c, c.getName());
	    if (defaultCountry != null && foundDefault == false) {
		log.debug(m, "Checking <%s> for match to <%s>",
		          c.getName(), defaultCountry.getName());
		if (c.getName().equals(defaultCountry.getName())) {
		    log.debug(m, "Set default to <%s>", c.getName());
		    cb.setValue(c);
		    foundDefault = true;
		}
	    }
	}

The code for Country is attached.

When the above code is run, the “Set default to <…>” debug line correctly reports the Country Name. Thus I know cb.setValue(c) is being called. However, the ComboBox shows no default value when painted.

I’ve also done a debug step through from cb.setValue() downward and saw no obvious problems.

I’ve tried using NativeSelect() but the result was the same.

Would really, really appreciate some pointers on this one. My head hurts from hitting the table so much. :slight_smile:

mike
12065.java (3.66 KB)

Hi

I just copy/paste your code (with your Pojo Country) in a Simple Vaadin project and it’s work verry well.

Here my test code :


public class Test extends Application
{
	    
	@Override
	public void init() {
		Window mainWindow = new Window("Application");
        setMainWindow(mainWindow);
        
        Country defaultCountry = new Country();
        defaultCountry.setName("Canada3");
       
        ComboBox cb = new ComboBox("Country");
        cb.setRequired(true);
        cb.setRequiredError("Please select the Country");
        cb.setNullSelectionAllowed(false);
       
        boolean foundDefault = false;
        // getSet returns a Set<Country> of all Country's
        for (Country c : getSet(false)) {
            cb.addItem(c);
            cb.setItemCaption(c, c.getName());
            if (defaultCountry != null && foundDefault == false)
            {
            	if (c.getName().equals(defaultCountry.getName()))
            	{
            		cb.setValue(c); // "Canada3" appears for value
            		foundDefault = true;
            	}
            }
        }

      mainWindow.addComponent(cb);
        
        
	}


	private Set<Country> getSet(boolean b) {
		Country c = new Country();
		c.setName("Canada");
		
		Country c1 = new Country();
		c1.setName("Canada2");
		
		Country c2 = new Country();
		c2.setName("Canada3");
		
		Set<Country> hashSet = new HashSet<Country>();
		hashSet.add(c);
		hashSet.add(c1);
		hashSet.add(c2);
		
		return hashSet;
	}
}

Your problem seem to be elsewhere.

How do add your comboBox to your layout ? (or how do you use this ComboBox ? )

Eric,

Thanks for taking the time to whip up a test case. That test case also works for me. However, below is a full test case which shows the problem.

Hopefully somebody has some suggestions on this? I’m starting to just hit my head with a hammer and that seems to cause a bit of pain. :slight_smile:


package com.example.testcomboboxcountry;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.*;

@SuppressWarnings("serial")
public class Testcombobox extends Application {
    public Window mainWindow;
    private Address address;
    private Country defaultCountry = getSet().iterator().next(); // 1st country
    private Object [] addrIds = {
                                 "address1",
                                 "city",
                                 "country",
    };
    private Set<Object> addressIds = new LinkedHashSet<Object>(Arrays.asList(addrIds));
    
    @Override
    public void init() {

	mainWindow = new Window("Test ComboBox");
	setMainWindow(mainWindow);

	address = new Address();
	address.setAddress1("123 Nowhere St");
	address.setCity("LA");
	address.setCountry(defaultCountry);

	System.out.printf("Default country=<%s>\n", defaultCountry.getName());
	
	Form form = makeForm(address);
	mainWindow.addComponent(form);
	
    }

    private Form makeForm(Address address) {
	
	Form form = new Form();
	form.setFormFieldFactory(new MyFieldFactory());
	form.setItemDataSource(new BeanItem<Address>(address),
	                       addressIds);
	
	return form;
    }

    /**
     * Custom field factory which allows us to specially format fields.
     */
    private class MyFieldFactory extends DefaultFieldFactory {
	//private Trace	     log	      = TraceFactory.getTrace(MyFieldFactory.class);

	@Override
	public Field createField(Item item, Object propertyId,
	                         Component uiContext) {

	    Field field = super.createField(item, propertyId, uiContext);
	    Property prop = item.getItemProperty(propertyId);
	    
	    if (prop.getType().equals(Country.class)) {

		ComboBox cb = new ComboBox("Country");

		boolean foundDefault = false;
		// getSet returns a Set<Country> of all Country's
		for (Country c : getSet()) {
		    cb.addItem(c);
		    cb.setItemCaption(c, c.getName());
		    if (defaultCountry != null && foundDefault == false) {
			if (c.getName().equals(defaultCountry.getName())) {
			    System.out.printf("Setting ComboBox default to <%s>\n", c.getName());
			    cb.setValue(c); // "Canada1" appears for value
			    foundDefault = true;
			}
		    }
		}
		return cb;
	    } 

	    return field;
	}
    }
    
    private Set<Country> getSet() {
	Country c1 = new Country();
	c1.setName("Canada1");

	Country c2 = new Country();
	c2.setName("Canada2");

	Country c3 = new Country();
	c3.setName("Canada3");

	Set<Country> hashSet = new LinkedHashSet<Country>();
	hashSet.add(c1);
	hashSet.add(c2);
	hashSet.add(c3);

	return hashSet;
    }
}

Attached are the required data model classes.
12068.java (8.27 KB)
12069.java (5.31 KB)
12070.java (3.66 KB)

Hi Mike,

I dont know why I did not found it with your previous post.

You forgot to override equals and hashCode in your class Country.
I did not look, but I think your values of the ComboBox are stored in a Set. Vaadin must verify that the value exist in all of your values, so maybe a call to HashSet.contains(object) and your class Country return false because equals was not overriden.

I made this change in your testCase and works “like a charm”

Not anymore :grin:

Regards.

Éric

Eric,

Bingo!!! Implementing equals() worked for me too! Thank you! Thank you! Thank you!

mike