Using the Observable API

An Observable is an interface which allows you to subscribe to the change event of the corresponding property. All JavaFX properties are observable, thus you can track changes of every small parameter of every JavaFX node using the Observable API.

Let's take a look at the next simple example, which dynamically updates the width of the JavaFX application's window.

The window, represented by the Stage class, has a property called widthProperty, which we can listen to:

// chapter3/basics/WidthObservable.java
public void start(Stage stage) {
Label lblWidth = new Label();

// Note, we are not using any binding yet
stage.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> o, Number oldVal, Number newVal) {
lblWidth.setText(newVal.toString());
}
});

stage.setScene(new Scene(new StackPane(lblWidth), 200, 150));
stage.show();
}

Here is a screenshot:

Every property in JavaFX is implementing the similar ObservableValue interface and can be listened to for changes. 

Not clearing listeners properly is a common reason for Java memory leaks. To address that, JavaFX supports weak-reference for all listeners, so if you are working on a large application, consider using WeakListener descendants and WeakEventHandler

Besides plain values, you can observe collection types through the following interfaces extending Observable:

  • ObservableList: java.util.List with the Observable API
  • ObservableMap: java.util.Map with the Observable API
  • ObservableArray: A class that encapsulates a resizable array and adds the Observable API

Overall, there are over 100 classes implementing Observable in JavaFX.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.249.174