2017-07-08 124 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(由於比較失敗)不會向監聽器發送任何 通知,使其再次生效。

JavaDoc of javafx.collections.transformation.SortedList

tutorial可能會有幫助。

相關問題