java 不同属性的双向绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14138082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bidirectional bindings of different properties
提问by dethlef1
I just tried to bind a Integer and a String property. After some googling this should be possible with one of the two provided methods:
我只是尝试绑定一个 Integer 和一个 String 属性。经过一些谷歌搜索后,这应该可以使用提供的两种方法之一:
public static void bindBidirectional(Property stringProperty,
Property otherProperty, StringConverter converter)public static void bindBidirectional(Property stringProperty,
Property otherProperty, java.text.Format format)
public static void bindBidirectional(属性stringProperty,
属性otherProperty ,StringConverter 转换器)public static void bindBidirectional(Property stringProperty,
Property otherProperty , java.text.Format format)
Unluckily this does not seem to work for me. What am I doing wrong?
不幸的是,这似乎对我不起作用。我究竟做错了什么?
import java.text.Format;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.util.converter.IntegerStringConverter;
public class BiderectionalBinding {
public static void main(String[] args) {
SimpleIntegerProperty intProp = new SimpleIntegerProperty();
SimpleStringProperty textProp = new SimpleStringProperty();
Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());
intProp.set(2);
System.out.println(textProp);
textProp.set("8");
System.out.println(intProp);
}
}
回答by Emily L.
Simple matter of type confusion
类型混淆的简单问题
Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());
Should be:
应该:
Bindings.bindBidirectional(textProp, intProp, new NumberStringConverter());
回答by Hendrik Ebbers
I tried your code in Eclipse and had to cast the converter. Then everything looks ok:
我在 Eclipse 中尝试了你的代码,不得不转换转换器。然后一切正常:
public class BiderectionalBinding {
public static void main(String[] args) {
SimpleIntegerProperty intProp = new SimpleIntegerProperty();
SimpleStringProperty textProp = new SimpleStringProperty();
StringConverter<? extends Number> converter = new IntegerStringConverter();
Bindings.bindBidirectional(textProp, intProp, (StringConverter<Number>)converter);
intProp.set(2);
System.out.println(textProp);
textProp.set("8");
System.out.println(intProp);
}
}
The output is:
输出是:
StringProperty [value: 2]
StringProperty [值:2]
IntegerProperty [value: 8]
IntegerProperty [值:8]
回答by Daniel Ludwig
i had a similar problem. I tried to convert a string into a File-Object and back. But i used Bindings.bindBidirectional(...,...,java.text.Format). The conversion from the string to file worked as expected but in the other direction the result was null. I tried it with your example, same Result! I think there is a bug in the binding mechanism, or maybe my implementation of java.text.Format is wrong..
我有一个类似的问题。我试图将字符串转换为文件对象并返回。但我使用了 Bindings.bindBidirectional(...,...,java.text.Format)。从字符串到文件的转换按预期工作,但在另一个方向上结果为空。我用你的例子试过了,结果一样!我认为绑定机制存在错误,或者我对 java.text.Format 的实现是错误的..
package de.ludwig.binding.model;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class BidirectionalBinding {
public static void main(String[] args) {
SimpleIntegerProperty intProp = new SimpleIntegerProperty();
SimpleStringProperty textProp = new SimpleStringProperty();
Bindings.bindBidirectional(textProp, intProp, new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
return toAppendTo.append(obj.toString());
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return Integer.parseInt(source);
}
});
intProp.set(2);
System.out.println(textProp);
textProp.set("8");
System.out.println(intProp);
}
}
The only way to get things working as expected was to implement StringConverter as recommended by Hendrik Ebbers. Thank you for this tip!
使事情按预期工作的唯一方法是按照 Hendrik Ebbers 的建议实施 StringConverter。谢谢你的提示!
回答by Uluk Biy
I think that is a bug. Anyway you can workaround like:
我认为这是一个错误。无论如何,您可以解决以下问题:
StringConverter sc = new IntegerStringConverter();
Bindings.bindBidirectional(textProp, intProp, sc);