0

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?

divibisan
  • 11,659
  • 11
  • 40
  • 58
Débora
  • 5,816
  • 28
  • 99
  • 171
  • sounds like the conversion string --> number can't handle null/empty input (which most formatters cant, you'll have to tell them or your converter to accept that a 0) – kleopatra Sep 21 '11 at 15:57
  • I am still new to beansbinding. Would you let me know with sample converter class how to do this? – Débora Sep 21 '11 at 16:12
  • What I think is,when there is no any character in JTextField (when there had been some characters earlier) the JtextField's value is "" not null or empty.That is how I think and don't know exact reasone. – Débora Sep 21 '11 at 16:23
  • well, "" is the empty String :-) Map both null and empty String to zero and be happy ... – kleopatra Sep 21 '11 at 16:29

1 Answers1

1

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);
    }
};
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Thanks for your codes. I used it in my application.It worked me well. Thank you so much :) I had another problem in converters as well. I could solve that one also with your ideas. Very happy :) – Débora Sep 21 '11 at 18:44