java - Why do we need converters for TextFormatters -
my professor said 'ideal' use filter , converter textformatter. looked @ examples , tried them, couldn't understand why need converter @ all.
from docs:
a formatter describes format of textinputcontrol text using 2 distinct mechanisms:
a filter (getfilter()) can intercept , modify user input. helps keep text in desired format. default text supplier can used provide intial text.
a value converter (getvalueconverter()) , value (valueproperty()) can used provide special format represents value of type v. if control editable , text changed user, value updated correspond text.
i cleary missing here. why want convert string integer (for calculations etc.). why have have part of textformatter? can't use gettext() , cast text want have value?
one more thing: if have filter doesn't allow non-numeric characters, why need take care of conversion of text integer/double etc. converter?
maybe missing obvious.
you can't cast string integer (or other type, except object): have convert it. if text formatter has filter allows numeric entry, text field's gettext() method still returns string, not convenient (as entry in text field represents numeric value in object).
you might need integer (for example) value represented text field in many different places, centralize conversion code in 1 place including converter part of formatter.
additionally, formatter's value observable property, can bind other properties it, etc. tricky if needed perform conversion in binding on text field's text property.
Comments
Post a Comment