Not getting expected results from a ListChangeListener on a TreeView selection model

82 views Asked by At

I have the following test code below:

    tvNodes.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    tvNodes.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<TreeItem<Node>>() {
                @Override
                public void onChanged(Change<? extends TreeItem<Node>> c) {
                    while (c.next()) {
                        log.debug("From {} To {} (Removed: {}, Added: {}, Replaced: {}, List: {})", c.getFrom(), c.getTo(), c.wasRemoved(), c.wasAdded(), c.wasReplaced(), c.getList());
                        if (c.wasAdded()) {
                            for (var added : c.getAddedSubList()) {
                                log.debug("\tADDED ITEM: {}", added);
                            }
                        }
                        if (c.wasRemoved()) {
                            for (var removed : c.getRemoved()) {
                                log.debug("\tREMOVED ITEM: {} ({})", removed.getValue(), c.getRemovedSize());
                            }
                        }
                        if (c.wasReplaced()) {
                            log.debug("\tWAS REPLACED {} to {}", c.getFrom(), c.getTo());
                        }
                        if (c.wasPermutated()) {
                            log.debug("\tWAS PERMUTATED {} to {}", c.getFrom(), c.getTo());
                        }
                    }
                }
            });

When transitioning from a "full selection" to a "single item selection" (Circle 8)

From:

Full Selection

To:

Single Selection

I expected to receive two change events (Remove). The first event returns items from Circle (3) to Circle (7) and the second event returns items from Circle (9) to Circle (11).

Actual Results:

The first event returns the first half as expected, but the second event returns items 4,5,6 instead of 9, 10 and 11.

From 0 To 0 (Removed: true, Added: false, Replaced: false, List: [TreeItem [ value: Circle (8) ]])
    REMOVED ITEM: Circle (3) (5)
    REMOVED ITEM: Circle (4) (5)
    REMOVED ITEM: Circle (5) (5)
    REMOVED ITEM: Circle (6) (5)
    REMOVED ITEM: Circle (7) (5)
From 1 To 1 (Removed: true, Added: false, Replaced: false, List: [TreeItem [ value: Circle (8) ]])
    REMOVED ITEM: Circle (4) (3)
    REMOVED ITEM: Circle (5) (3)
    REMOVED ITEM: Circle (6) (3)

Is this a bug or am processing the results incorrectly? I'm currently using JDK 15.0.036 with OpenJFX 15

0

There are 0 answers