I want to bind
dp_date_add.valueProperty().bindBidirectional(model.forDateProperty());
where forDateProperty()
is:
public ObjectProperty<Date> forDateProperty() {
if(forDate == null){
forDate = new SimpleObjectProperty<>();
}
return forDate;
}
The problem is bindBiderectional
accpets only LocalDate
. I've tried this:
dp_date_add.valueProperty().bindBidirectional(model.forDateProperty().get().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
But it doesn't works, because it convert to LocalDate, not to Property LocalDate.
答案 0 :(得分:2)
如果可以,解决此问题的简便方法是更改模型,使其使用ObjectProperty<LocalDate>
。假设你不能这样做,你需要使用两个听众:
dp_date_add.valueProperty().addListener((obs, oldDate, newDate) ->
model.forDateProperty().set(Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant())));
model.forDateProperty().addListener((obs, oldDate, newDate) ->
dp_date_add.setValue(model.forDateProperty().get().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()));