Есть коллекция с объектами . На нее вешается листенер
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
destinations.addListener((ListChangeListener<Destination>) c -> {
while (c.next()) {
if (c.wasAdded()) {
for (Destination added : c.getAddedSubList()) {
SimpleObjectProperty<Destination> property = new SimpleObjectProperty(added);
property.addListener(destinationListener);
getChildren().add(new Item(NEW, property, getPrefWidth(), getPrefHeight()));
}
}
}
});
Кусок кода
1.
getChildren().add(new Item(NEW, property, getPrefWidth(), getPrefHeight()));
добавляет новый элемент в TilePane. Тот листенер, который вешается на проперти:
1.
2.
3.
4.
5.
6.
7.
(observable, oldValue, newValue) -> {
if (Objects.isNull(newValue)) {
Node item = getChildren().stream().filter(n -> ((tem) n).getDestination() == null).findFirst().get();
getChildren().remove(item);
destinations.remove(oldValue);
}
};
Получается, если мы меняем значение проперти на null, должен выполниться этот кусок кода. На строчке, которая бы удаляла item из TilePane
1.
getChildren().remove(item);
рыгается exception
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = RouteList@5df8ab3a[styleClass=tile-pane]
at javafx.scene.Parent$2.onProposedChange(Parent.java:450)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:329)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:221)
at com.RouteList.lambda$new$1(RouteList.java:25)
at com.RouteList$$Lambda$99/1993147782.changed(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
at com.RouteListItem.lambda$addRectangle$2(RouteListItem.java:70)
at com.RouteListItem$$Lambda$244/578608980.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
Какого *???