Binding Javafx StringProperty中的双向绑定没有反向工作

Binding Javafx StringProperty中的双向绑定没有反向工作,binding,javafx,Binding,Javafx,我有一个经典的JavaBean,它将与JavaFX文本字段绑定 public class Cell { public static final String CELL_VALUE = "Cell.Value"; private Optional<Integer> value; private final PropertyChangeSupport pcs = new PropertyChangeSupport(this); public Op

我有一个经典的JavaBean,它将与JavaFX文本字段绑定

public class Cell {

    public static final String CELL_VALUE = "Cell.Value";

    private Optional<Integer> value;

    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);


    public Optional<Integer> getValue() {
        return value;
    }

    public void setValue(Optional<Integer> value) {
        Optional<Integer> old = this.value;
        this.value = value;
        this.pcs.firePropertyChange(CELL_VALUE, old, value);
    }

    /**
     * The values must be from 1 to 9. 0 or null will be converted to Option.none.
     */
    public void setValue(int value) {
        this.setValue(Optional.of(value));
    }

}
如果我在这个文本字段中输入“8”,我会得到:

fromString() : 
cell         : Optional[4] -> Optional.empty
valueProperty: Optional[4] -> Optional.empty
textProperty : 4 -> 
fromString() : 8
cell         : Optional.empty -> Optional[8]
valueProperty: Optional.empty -> Optional[8]
textProperty :  -> 8
最后,如果我键入“0”,单元格将变为空:

fromString() : 
cell         : Optional[8] -> Optional.empty
valueProperty: Optional[8] -> Optional.empty
textProperty : 8 -> 
到目前为止,一切顺利。但如果我双击文本字段,而不是替换文本,则什么也不会发生。假设单元值(以及texfField)为4。当我双击时,我收到以下消息:

cell         : Optional[4] -> Optional[8]
但是,文本字段继续显示“4”。
CellValueStringConverter.toString()
中的消息未显示

据推测,当我将单元格值包装在
ObjectProperty
JavaBeanObjectPropertyBuilder.create().bean(cell.name(“value”).build()
)中时,它应该会观察value属性中的所有更改。但这并没有发生。这里少了什么

谢谢


拉斐尔·阿方索(Rafael Afonso)

我想我找到了答案,或者至少找到了解决办法。我必须向我的
单元格
对象添加一个新的
PropertyChangeEvent
,当
单元格.value
attibute更改时,直接设置valueProperty:

cell.addPropertyChangeListener(evt -> {
    @SuppressWarnings("unchecked")
    final Optional<Integer> newValue = (Optional<Integer>) evt.getNewValue();
    valueProperty.set(newValue);
});
文本字段按预期填充。然而,我觉得奇怪的是valueProperty消息是最后一个被打印出来的,尽管他的setter是第一个被调用的东西

如果有人有更好的主意,我们欢迎

fromString() : 
cell         : Optional[8] -> Optional.empty
valueProperty: Optional[8] -> Optional.empty
textProperty : 8 -> 
cell         : Optional[4] -> Optional[8]
cell.addPropertyChangeListener(evt -> {
    @SuppressWarnings("unchecked")
    final Optional<Integer> newValue = (Optional<Integer>) evt.getNewValue();
    valueProperty.set(newValue);
});
cell         : Optional.empty -> Optional[2]
toString()   : Optional[2]
textProperty :  -> 2
valueProperty: Optional.empty -> Optional[2]