官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 昌平区| 吴堡县| 天柱县| 梅州市| 崇义县| 满洲里市| 元江| 炎陵县| 临清市| 商南县| 固始县| 东乡族自治县| 剑河县| 迁安市| 连云港市| 石嘴山市| 平山县| 德惠市| 虎林市| 青浦区| 牡丹江市| 剑河县| 西畴县| 凯里市| 友谊县| 景德镇市| 六安市| 通城县| 闽侯县| 赤峰市| 贵阳市| 永城市| 晋城| 赤城县| 城口县| 镶黄旗| 楚雄市| 武义县| 腾冲县| 宜川县| 宁德市|