JavaFX bidirectional binding of two different, non

2019-08-08 07:51发布

问题:

I need to bind together two ListProperties:

ObservableList<Data<Number, Number>>    chartDataList       = FXCollections.observableArrayList();
ListProperty<Data<Number, Number>>      chartDataProperty   = new SimpleListProperty<>(this, "chartData", chartDataList);
ObservableList<Point2D>                 observableData      = FXCollections.observableArrayList();
ListProperty<Point2D>                   dataProperty        = new SimpleListProperty<>(this, "Data", observableData);

chartDataList will be used to display a LineChart, while the other (data) will be used in the rest of my program. chartDataProperty is not really needed (but for binding), because LineChart uses just chartDataList.

Note: binding needs to be bidirectional because I have means to manually drag the points on the LineChart using mouse.

I didn't find a direct way to handle this; only conversion seems to be to/from String (or ListProperty<String>). I could use that, with man-in-the-middle ListProperty<String>, but it looks very inefficient.

Otherwise I need to resort to ChangeListner usage, manually handling all possible cases.

Please tell me I have some better choice.