我有两个JtextFields叫做"qty“和"amount”。当用户输入qty时,该值将在某种计算下消失,并将最后一个值设置为amount文本字段。我已经将这两个文本字段绑定到beansbinding类的属性。当用户输入qty时,负责该文本字段的属性被调用,然后我调用了qty的firepropertychange以及数量的firepropertychange,以根据数量更新amount的值。当使用退格键删除qty的文本域的值时,这工作well.Also当qty文本域为空时,qty的值也是change.but的,数量文本域保留它的最后一个值(假设qty有一个数字'22‘,amount文本域显示'44',当按backspace时,数字是'2’,amount的显示值是'4',但是当qty中的最后一个值'2‘也被删除时,amount文本域显示为’4‘。.I希望amount文本域应该显示为零。
对此有什么解决方案吗?
发布于 2011-09-22 00:58:08
刚刚检查了默认的转换器:它们不处理null/empty,您必须实现一个可以处理的转换器,并将其设置为绑定。例如,要查看差异,请取消注释转换器设置:
@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);
}
};https://stackoverflow.com/questions/7502309
复制相似问题