Matt Perry
Matt Perry

Reputation: 343

Simple binding with JavaFX8

I have been exploring JavaFX8 for a few days and am trying to educate myself with the concept of binding. I've created an ObservableList like this

private static ObservableList<XYChart.Series<String, Number>> chartData = FXCollections.<XYChart.Series<String, Number>>observableArrayList();

I then go through a few steps to create an AreaChart and have had success calling the setAll() method below

areachart.getData().setAll(chartData);

It is my understanding though that bindings would allow me to remove this step by associating the data property with the list. If the list changes, the chart data property would automatically be "updated".

I've tried to bind the data property like this

areachart.dataProperty().bind(chartData);

However, its asking for a syntax that I'm not familiar with at all

(ObservableValue<? extends ObservableList<Series<String, Number>>> arg0)

Can someone please help me create the correct syntax to bind the list to the area chart? If I've gotten the concept of binding all mixed up please let me know.

Upvotes: 0

Views: 694

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49215

JavaFX's ObservableList is special List which can be observed for its children manipulations by attaching an event handler to it. Please read related javadocs for more info.

In JavaFX, the ancestor of all charts, XYChart has a dataProperty() which is ObjectProperty<ObservableList<XYChart.Series<X,Y>>>. Namely this is a property field that holds an object whose type is ObservableList. And this is the field you need to set its holding observable list directly as

areachart.setData(chartData);

Now the data of areachart and chartData are referencing the same list, any removing/adding to chartData will be "reflected" to areachart's data and vice versa.

The

areachart.getData().setAll(chartData);

is also valid but it will copy list-to-list. I.e. it will internally loop the chartData and copy its children (XYChart.Series in this case) into the data observable list of areachart. Resulting to different lists, so no relation, no "reflection" between them.

The

areachart.dataProperty().bind(chartData);

is wrong. Because it is trying to bind, roughly saying, the property<list> to list.

Even though the dataProperty is ObjectProperty<ObservableList<XYChart.Series<X,Y>>>, binding to it through dataProperty().bind() will expect ObservableValue<? extends ObservableList<Series<String, Number>>>, where ObservableValue is a superclass of ObjectProperty. This is by design, there were discussions on this topic on SO, but cannot remember the actual Q&A entry. Try to search them.

Before going deeper in JavaFX refer to Using JavaFX Properties and Binding and other resources to understand it better. Generally speaking property is a wrapper to its holding object, by providing an observer like pattern to its state changes and also providing flexibility to plug up to other property objects through JavaFX APIs. See for example Bindings and its usage examples on the net.

Upvotes: 2

Related Questions