2017-07-08 4 views
1

私はjavafxで作業しています。私はObservableListによって更新されるListviewを持っています。 ここに私のコードです。リストビューからオブジェクトを削除しようとすると例外がスローされます

//Dummy list for testing listview 
    final ObservableList<String> names = FXCollections.observableArrayList(
      "Julia", "Ian", "Sue", "Matthew", "Hannah", "Stephan", "Denise"); 


    final ListView<String> listView = new ListView(names.sorted()); 

    //Try to remove the first element for testing purposes 
    listView.getItems().remove(0); 

削除しようとすると、この例外が発生します。

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:748) 
Caused by: java.lang.UnsupportedOperationException 
    at java.util.AbstractList.remove(AbstractList.java:161) 
    at sample.Main.start(Main.java:39) 
    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) 
    ... 1 more 
Exception running application sample.Main 

この場合、いくつかのステップがありますか?またはObservableListとListViewを間違った方法で使用しようとしていますか?

答えて

1

javafx.collections.transformation.SortedList<E>が正しく使用されていません。

唯一のラッパー、ビューです。

要素を削除するには、ソースコレクション:namesを使用する必要があります。

javafx.collections.transformation.SortedList<E>remove()を実装していませんが:

はObservableListをラップし、それがコンテンツだソートします。 ObservableList内のすべての変更は、直ちにSortedListに伝播されます。注: 無効なSortedList(比較が壊れた結果として)は、有効になったときにリスナーに通知を送信しません。 このtutorialは役立つかもしれ

JavaDoc of javafx.collections.transformation.SortedList

関連する問題