Callback and extractors for JavaFX ObservableList

你说的曾经没有我的故事 提交于 2019-11-30 19:51:43
James_D

An ObservableList will fire change events when items are added and removed from the list, whether or not it is created with an extractor.

However, if items in the list either are, or contain references to, observable properties, the list will fire updates when those properties change only if it is constructed with an extractor which returns an array containing references to those properties.

This should demonstrate the difference:

import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;


public class ListExtractorDemo {
    public static void main(String[] args) {
        ObservableList<IntegerProperty> listWithoutExtractor = 
                FXCollections.observableArrayList();

        ObservableList<IntegerProperty> listWithExtractor = 
                FXCollections.observableArrayList(p -> new Observable[]{p});

        listWithoutExtractor.addListener(createListener("listWithoutExtractor"));
        listWithExtractor.addListener(createListener("listWithExtractor"));

        IntegerProperty p1 = new SimpleIntegerProperty(1);
        IntegerProperty p2 = new SimpleIntegerProperty(2);

        // both lists will fire change events when items are added or removed:
        listWithoutExtractor.addAll(p1, p2);
        listWithExtractor.addAll(p1, p2);

        // only the list with the extractor will fire a change event when the observable value of an element changes:
        p2.set(3);
    }

    private static ListChangeListener<IntegerProperty> createListener(String listId) {
        return (Change<? extends IntegerProperty> c) -> {
            while (c.next()) {
                if (c.wasAdded()) {
                    System.out.println(listId + " added: "+c.getAddedSubList());
                }
                if (c.wasRemoved()) {
                    System.out.println(listId + " removed: "+c.getRemoved());
                }
                if (c.wasUpdated()) {
                    System.out.println(listId + " updated");
                }
            }
        };
    }
}

A typical use case is in @kleopatra's answer to JavaFX 2.0 Choice Box Issue. How to update a choiceBox, which represents a list of objects, when an object is updated?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!