简体   繁体   English

JavaFX:适配器,ObjectProperty和双向绑定

[英]JavaFX: adapter, ObjectProperty and bidirectional binding

I want to understand how ObjectProperty works but still no luck. 我想了解ObjectProperty的工作原理,但还是没有运气。 I have the following POJO: 我有以下POJO:

public class Article{

    private LocalDateTime dateTime;

    private final PropertyChangeSupport propertyChangeSupport;

    public Article(String title) {
        this.propertyChangeSupport = new PropertyChangeSupport(this);
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public void setDateTime(LocalDateTime dateTime) {
        LocalDateTime pv = this.dateTime;
        this.dateTime = dateTime;
        propertyChangeSupport.firePropertyChange("dateTime", pv, this.dateTime);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
} 

Now, I create objectProperty for dateTime field: 现在,我为dateTime字段创建objectProperty:

ObjectProperty<LocalDateTime> dateTimeProperty=new JavaBeanObjectPropertyBuilder().bean(article).name("dateTime").build();

And now I want to bind this dateTimeProperty to textField.textProperty(). 现在,我想将此dateTimeProperty绑定到textField.textProperty()。 And I have no idea how to do it: how will the data be formatted for showing dateTime in textField? 而且我不知道该怎么做:如何格式化数据以在textField中显示dateTime? How the instance of LocalDateTime will be created when I enter new date time in textField? 当我在textField中输入新的日期时间时,如何创建LocalDateTime实例? We do have bidirectional binding. 我们确实有双向绑定。 Please, explain. 请解释。

Why not create your bean as a JavaFX Bean 为什么不将您的bean创建为JavaFX Bean

public class Article {
    private ObjectProperty<LocalDateTime> dateTime = new SimpleObjectProperty<>();
    public final ObjectProperty<LocalDateTime> dateTimeProperty() {
         return dateTime;
    }
    public void setDateTime(LocalDateTime ldt) {
         dateTime.set(ldt);
    }
    public LocalDateTime getDateTime() {
         return dateTime.get();
    }
}

Then in your control you bind to the text control with a formatter 然后在您的控件中,使用格式化程序将其绑定到文本控件

myTextField.textProperty().bindBidirectional(article.dateTimeProperty(), new LocalDateTimeStringConverter());

The best option is to use a TextFormatter on the text field: 最好的选择是在文本字段上使用TextFormatter

// choose whatever formats you need here...
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
DateTimeFormatter parser = DateTimeFormatter.ofPattern("d/M/y H:m:s");

TextFormatter<LocalDateTime> textFormatter = new TextFormatter(new LocalDateTimeStringConverter(formatter, parser));
myTextField.setTextFormatter(textFormatter);

and then 接着

textFormatter.valueProperty().bindBidirectional(dateTimeProperty);

If you want, you can also specify a filter on the TextFormatter , to limit editing to characters that only make sense in this context (though the details of that can get a bit tricky). 如果需要,您还可以在TextFormatter上指定一个过滤器,以将编辑限制为仅在这种情况下才有意义的字符(尽管其细节可能会有些棘手)。 Refer to the documentation for TextFormatter.Change for details. 有关详细信息,请参考TextFormatter.Change的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM