我想绑定dp_date_add.valueProperty().bindBidirectional(model.forDateProperty());
其中forDateProperty()
是:
public ObjectProperty<Date> forDateProperty() {
if(forDate == null){
forDate = new SimpleObjectProperty<>();
}
return forDate;
}
问题是
bindBiderectional
仅可容纳LocalDate
。我已经试过了:dp_date_add.valueProperty().bindBidirectional(model.forDateProperty().get().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
但这是行不通的,因为它会转换为LocalDate,而不是Property LocalDate。
最佳答案
如果可以的话,解决此问题的简单方法是更改模型,使其使用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()));