I have 2 JtextFields called "qty" and "amount". When the user types in qty, the value is gone under some calculation and set the last value to amount textfield. I have bound these 2 textfields to beansbinding class's properties. when the user types in qty, the property which is responsible for that textfield is called and then I have called the firepropertychange of the qty as well as the amount's firepropertychange to update the value of amount according to the qty. This works well.Also when qty's textfield's value is being deleted with backspace button the qty's value also change.but when qty textfield is empty, the amount textfield remains with its last value(let's say qty is having a number '22' and amount textfield shows '44', and when backspace is pressed the number is '2' and amount's showwing value is '4',but when the last value '2' in qty is aslo deleted, the amount textfield shows '4').I want that the amount textfield should be showing zero.
Any solution for this please?
just checked the default converters: they don't handle null/empty, you have to implement one that can do and set that to the binding. Something like, to see the difference uncomment the converter setting:
@SuppressWarnings({ "rawtypes", "unchecked" })
private void bind() {
BindingGroup context = new BindingGroup();
AutoBinding firstBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
// this is some int property
this, BeanProperty.create("attempts"),
fields[0], BeanProperty.create("text"));
context.addBinding(firstBinding);
// firstBinding.setConverter(INT_TO_STRING_CONVERTER);
context.bind();
}
static final Converter<Integer, String> INT_TO_STRING_CONVERTER = new Converter<Integer, String>() {
@Override
public String convertForward(Integer value) {
return Integer.toString(value);
}
@Override
public Integer convertReverse(String value) {
if (value == null || value.trim().length() == 0) return 0;
return Integer.parseInt((String) value);
}
};