2016-05-25 35 views
2

我试图将DTO上的ObjectProperty<LocalDate>绑定到TextField,并且无法使其工作。如何在JavaFX中双向绑定LocalDate到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)

不太一定要尝试下呢?

回答

2

根据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

(粗体是我的)

所以,你应该做相反的顺序绑定,绑定是来自文本框的“其他”的StringProperty财产,生日财产:

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

我建议使用DatePicker来代替。然后您可以绑定value property

+0

不幸的是我的用户不希望日期选择器,所以我坚持这个用例。 –