2017-12-05 4 views
0

LiveList을 사용하여 그룹의 자식을 자식 데이터가 들어있는 목록에 바인딩합니다. 여기에 예입니다업데이트 할 때 LiveList가 두 번 호출됩니다.

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     ObservableList<Integer> intList = FXCollections.observableArrayList(); 
     LiveList<Circle> circleList = LiveList.map(intList, i -> { 
      System.out.println("in"); 
      return new Circle(i); 
     }); 

     Group group = new Group(); 
     Bindings.bindContent(group.getChildren(), circleList); 

     intList.add(2); 
     intList.clear(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

내 문제는 intList의 각 변화에 대한 바인딩 목록이 두 번 업데이트 및 필요한 것보다 더 많은 개체를 생성하는 점이다. 코드를 실행하면 제공 :

in 
in 
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = [email protected] 
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454) 
    at com.sun.javafx.collections.VetoableListDecorator$VetoableSubListDecorator.clear(VetoableListDecorator.java:529) 
    at com.sun.javafx.binding.ContentBinding$ListContentBinding.onChanged(ContentBinding.java:114) 
    at org.reactfx.collection.ChangeListenerWrapper.onChange(LiveList.java:439) 
    at org.reactfx.collection.ChangeListenerWrapper.onChange(LiveList.java:417) 
    at org.reactfx.util.ListNotifications.lambda$takeHead$0(NotificationAccumulator.java:317) 
    at org.reactfx.ObservableBase.notifyObservers(ObservableBase.java:68) 
    at org.reactfx.ObservableBase.notifyObservers(ObservableBase.java:57) 
    at org.reactfx.collection.MappedList.sourceChanged(MappedList.java:41) 
    at org.reactfx.collection.LiveList.lambda$observeQuasiChanges$7(LiveList.java:256) 
    at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164) 
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73) 
    at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233) 
    at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482) 
    at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541) 
    at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205) 
    at com.sun.javafx.collections.ObservableListWrapper.clear(ObservableListWrapper.java:157) 
    at test1.Main.start(Main.java:27) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    at java.lang.Thread.run(Thread.java:748) 

내가 그룹의 아이들에 Circle있을 것 intList의 각 요소에 대해 그것을 원하지. 왜 이런 일이 일어나고 어떻게 제대로 작동하게 할 수 있습니까?

답변

0

예외 설명은 빨간색 청어입니다. 변경 사항이 끝나면 변경 사항이 예상 한 것과 일치하지 않을 때마다 "duplicate children added"메시지가 나타납니다. 귀하의 경우에는
, 무슨 실제로 발생하면 원이 아닌 이유 원 Group이 하나의 아이로 끝나는 있도록를 제거되지 않습니다, 아직 clear 그것이 0

이유와 함께 종료해야 예측이다 매핑을 수행하라는 메시지가 표시 될 때마다 매퍼에서 새 Circle 개체를 만들고 추가 할 때 1 개의 서클을 가져오고 목록을 지울 때 전혀 다른 서클을 만듭니다.이 새로운 Circle은 동일한 서클이 아닙니다. 그룹의 하위 그룹에서 삭제되므로 clear을 호출 한 후에도 1 명의 자녀가 남아 있으므로 예외가 트리거됩니다.

이 문제를 해결하려면 동일한 원본 개체에 대해 동일한 대상 개체를 반환해야합니다. 정수 목록에 중복 된 항목이 포함되어 있으면 가능하지 않을 수 있습니다.

+0

하지만 2 개의 동그라미가 'equals()'입니까? – Mordechai

+0

왜 그럴까요? – Itai

+0

하지만이 메서드가 두 번 호출되는 이유는 무엇입니까? – Mark

관련 문제