purring pigeon
purring pigeon

Reputation: 4209

How to bind a LocalDate to a TextField bidirectionally in JavaFX

I am trying to bind an ObjectProperty<LocalDate> on a DTO to a TextField and am having trouble getting this to work.

This is how I have it set up:

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

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

But this is giving me the following compile error:

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

Not quite sure what to try next?

Upvotes: 2

Views: 1493

Answers (2)

Jos&#233; Pereda
Jos&#233; Pereda

Reputation: 45456

According to the 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

(bold is mine)

So you should do the binding in the opposite order, to bind the StringProperty that comes from the textfield to the "other" property, the birthday property:

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

Upvotes: 3

Puce
Puce

Reputation: 38132

I recommend to use a DatePicker instead. You can then bind the value property.

Upvotes: 0

Related Questions