如何在不使用 a 的情况下将ObjectProperty
的属性(它本身不是属性)绑定到其他属性,例如 aTextField
的 text 属性ChangeListener
?
进一步来说:
我想TextField
更改一个ObjectProperty
's 属性。
示例代码:
地图DTO:
public class MapDTO {
private String cityName;
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
}
地图管理器:
public class MapsManager {
private static ObjectProperty<MapDTO> map = new SimpleObjectProperty<>();
public static MapDTO getMap() {
return map.get();
}
public static ObjectProperty<MapDTO> mapProperty() {
return map;
}
public static void setMap(MapDTO map) {
MapsManager.map.set(map);
}
}
绑定测试控制器:
public class BindingTestController {
private TextField cityNameTF = new TextField();
private void initialize() {
// Bind the cityName label to the selected MapsManager mapProperty's cityName
cityNameTF.textProperty().bind(Bindings.createStringBinding(
() -> MapsManager.mapProperty().getValue() == null ? null :
MapsManager.mapProperty().getValue().getCityName(),
MapsManager.mapProperty()));
}
}
我努力了:
从选定的值 String 属性创建一个字符串属性,但它没有成功,我无法找到正确的方法。
cityNameTF.textProperty().bindBidirectional(Bindings.createStringBinding(
() -> selectMapCB.getValue() == null ? null : selectMapCB.getValue().getCityName(),
selectMapCB.valueProperty()));
从 mapProperty 的 String 属性创建一个字符串属性。
cityNameTF.textProperty().bindBidirectional(Bindings.createStringBinding(
() -> MapsManager.getMapProperty().getValue() == null ? null : MapsManager.mapProperty().getValue().getCityName(),
MapsManager.mapProperty()));
两个选项都给出相同的错误:
bindBidirectional (javafx.beans.property.Property<java.lang.String>)
in StringProperty cannot be applied to (javafx.beans.binding.StringBinding)
在这两种情况下都替换bindBidirectional
为bind
作品,但我无法更改TextField
. 我意识到这是因为我将TextField
' 文本绑定到cityName
' 字符串。所以我想以一种方式绑定它,但方向相反,比如:
MapsManager.mapProperty().????.bind(cityNameTF.textProperty());
但 ”????” - 我没有字符串的属性,如果可能的话,我不知道如何创建一个StringBinding
或动态。StringProperty
如何在ObjectProperty
's 属性和另一个 's 属性之间手动创建字符串绑定StringProperty
?