java - Problems trying casting -
i've class extends textfield. class transform text uppercase , i'm trying casting class using beanfieldgroup doesn't work. using textfield works, class extends textfield doesn't.
/** class transfor text 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 textupper textupper name = (textupper)binder.getfield("name"); form.addcomponent(name);
any idea ???
i guess binder.getfield() returns textfield. after textupper name = (textupper)binder.getfield("name"); it'll still textfield, can check
name instanceof textupper
the cast not change type. "i know textupper, special type of textfield", isn't, made like. if cast , call methods afterwards, object still call textfield's methods, , not textupper ones. have create new textupper, set the text (and other values) textfield , use object if want have behaviour.
or, 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 strong feeling buildandbind calls parameter less constructor, not put parameter length anywhere
Comments
Post a Comment