Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

ComboBox/JPAContainer

Brian Sanders
1 decade ago Apr 27, 2010 2:23pm
Brian Sanders
1 decade ago Apr 27, 2010 9:35pm
Petter Holmström
1 decade ago Apr 28, 2010 5:20am
Brian Sanders
1 decade ago Apr 28, 2010 10:33am
Brian Sanders
1 decade ago Apr 29, 2010 3:33am
Brian Sanders
1 decade ago Apr 29, 2010 3:40pm

Woohoo! The solution was surprisingly simple. I created an anonymous subclass of CombBox and overrided getValue()

Brian Sanders
1 decade ago Jun 15, 2010 8:02pm

Here's my solution (you will need to adapt slightly). Yes, some of the code may look similar to what's in JPAContainer, but let's not depend on that implementation.

private class EntityComboBox extends ComboBox {
    
    private Class<? extends AbstractEntity<?>> entityClass;
    
    public EntityComboBox(String caption, Container dataSource, Class<? extends AbstractEntity<?>> entityClass) {
        super(caption, dataSource);
        this.entityClass = entityClass;
        setImmediate(true);
        setItemCaptionPropertyId("value");
        setNullSelectionAllowed(false);
    }
    
    @Override
    public Class getType() {
        return entityClass;
    }
    
    @Override
    protected void setInternalValue(Object newValue) {
        if (newValue != null 
				&& !entityClass.isAssignableFrom(newValue.getClass())) {
            try {
                newValue = entityClass
                            .getConstructor(new Class[] {String.class})
                                .newInstance(new Object[] {newValue});
            } catch (Exception e) {
                throw new ConversionException(e);
            }
        }
        
        super.setInternalValue(newValue);
    }
    
    @Override
    public boolean isSelected(Object itemId) {
        return itemId != null && itemId.equals(((AbstractEntity<?>) getValue()).getId()); 
    }
    
}
Roberto Neto
9 years ago Aug 28, 2012 8:38pm
Matti Tahvonen
9 years ago Aug 29, 2012 6:55am
Roberto Neto
9 years ago Aug 29, 2012 12:00pm
Matti Tahvonen
9 years ago Aug 29, 2012 4:03pm
Roberto Neto
9 years ago Aug 29, 2012 8:53pm
Matti Tahvonen
9 years ago Aug 30, 2012 7:37am
Marcos de Mello
8 years ago May 15, 2013 8:30pm