我只是尝试绑定(bind)一个 Integer 和一个 String 属性。经过一些谷歌搜索后,这应该可以通过提供的两种方法之一实现:
public static void bindBidirectional(Property stringProperty,
属性otherProperty,StringConverter转换器)public static void bindBidirectional(Property stringProperty,
属性otherProperty,java.text.Format格式)
不幸的是,这似乎对我不起作用。我做错了什么?
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);
}
}
最佳答案
简单的类型混淆问题
Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());
应该是:
Bindings.bindBidirectional(textProp, intProp, new NumberStringConverter());
关于java - 不同属性的双向绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14138082/