date - 如何在 JavaFX 中将 LocalDate 双向绑定(bind)到 TextField

标签 date binding javafx-8

我正在尝试绑定(bind) ObjectProperty<LocalDate>在 DTO 上到 TextField我很难让它发挥作用。

这就是我的设置方式:

dateOfBirthTextField.setTextFormatter(new TextFormatter<>(new LocalDateEnhancedStringConverter()));

person.birthdayProperty().bindBidirectional(dateOfBirthTextField.textProperty(), new LocalDateEnhancedStringConverter());

但这给了我以下编译错误:

The method bindBidirectional(Property) in the type ObjectProperty is not applicable for the arguments (StringProperty, LocalDateEnhancedStringConverter)

不太确定下一步要尝试什么?

最佳答案

根据 JavaDoc:

public <T> void bindBidirectional(Property<T> other, StringConverter<T> converter)

Create a bidirectional binding between this StringProperty and another arbitrary property. Parameters:

other - the other Property

converter - the StringConverter used to convert between this StringProperty and the other Property

(粗体是我的)

所以你应该以相反的顺序进行绑定(bind),绑定(bind) StringProperty来自文本字段到“其他”属性,即生日属性:

dateOfBirthTextField.textProperty()
    .bindBidirectional(
        person.birthdayProperty(), 
        new LocalDateEnhancedStringConverter());

关于date - 如何在 JavaFX 中将 LocalDate 双向绑定(bind)到 TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37447087/

相关文章:

MySQL:从字符串中提取日期

java - 解析时间戳包含时区到Date对象后,Date对象是否包含时区信息

javafx - 将 JavaFX FileChooser 限制为初始文件夹

JavaFX 动态 TableView 不显示数据库中的数据

SQL:日期字段集中的最早日期

javascript - 如何在 Javascript 中设置日期格式?

javascript - jQuery 绑定(bind)提交导致问题

WPF 绑定(bind)到 UserControl 自定义 DependencyProperty

c# - WPF 用户控件内的绑定(bind)

css - 如何摆脱 JavaFX Textview 周围的白色边框