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.javapublic 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<? ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.