Question

I've a class that extends TextField. This class transform the text to uppercase and now I'm trying do a casting with this class using BeanFieldGroup but doesn't work. Using TextField works, but with my class that extends TextField doesn't.

/** class transfor text to uppercase */
public class TextUpper extends TextField implements TextChangeListener{

    public TextUpper(Integer length){
        super();
        setMaxLength(length);   
        setImmediate(true);
        setTextChangeEventMode(TextChangeEventMode.EAGER);
        addTextChangeListener(this);
    }

    public TextUpper(){
        super();
    }

    @Override
    public void textChange(TextChangeEvent event) {
        String text = event.getText();
        setValue(text.toUpperCase());
    }
}


binder = new BeanFieldGroup<MyBean>(MyBean.class);
binder.setItemDataSource(ag);
Field<?> field = null;

Field field = binder.buildAndBind("Name", "name");
// Casting with TextUpper
TextUpper name = (TextUpper)binder.getField("name");
form.addComponent(name);

Any idea ???

Was it helpful?

Solution

I guess binder.getField() returns a TextField. So after TextUpper name = (TextUpper)binder.getField("name"); it'll be still a TextField, you can check this by

name instanceof TextUpper

The cast does not change its type. You only say "I know this is a TextUpper, a special type of TextField", but it isn't, you only made it look like. So if you do the cast and call methods afterwards, the object will still call the TextField's methods, and not the TextUpper ones. You have to create a new TextUpper, set the the text (and other values) from the TextField and use this object if you want to have its behaviour.

Or, just use

buildAndBind(String caption,
                                        Object propertyId,
                                        Class<T> fieldType)

with TextUpper :)

https://vaadin.com/download/prerelease/7.0/7.0.0/7.0.0.alpha3/docs/api/com/vaadin/data/fieldgroup/FieldGroup.html#buildAndBind(java.lang.String, java.lang.Object, java.lang.Class)


Try this:

 public TextUpper(Integer length){
        super();
        setMaxLength(length);   
        setImmediate(true);
        setTextChangeEventMode(TextChangeEventMode.EAGER);
        addTextChangeListener(this);
    }

    public TextUpper(){
        super(); 
        setImmediate(true);
        setTextChangeEventMode(TextChangeEventMode.EAGER);
        addTextChangeListener(this);
    }

I have the strong feeling that buildAndBind calls the parameter less constructor, as you do not put the parameter for length anywhere

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top