簡體   English   中英

從可觀察列表中刪除項目

[英]remove item from observable list

我有一個listVeiw ,它是一個HBox列表,帶有一個按鈕,用於從listView中刪除該HBox

但問題是當我按下那個按鈕時,它會拋出異常。 我嘗試從另一個不在HBox內的按鈕中刪除listView的條目,但它起作用了。

myObservableList.remove(lastIndex);

但我希望每個HBox內有一個刪除按鈕來刪除該條目,而不是使用全局按鈕來刪除最后一個條目。

編輯:添加例外:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3597)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3899)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1885)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2618)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
    ... 39 more
Caused by: java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:372)
    at java.base/java.util.ArrayList.remove(ArrayList.java:535)
    at javafx.base/com.sun.javafx.collections.ObservableListWrapper.doRemove(ObservableListWrapper.java:116)
    at javafx.base/javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:179)
    at http_client.ui.main.center_panel.header.HeaderController.deleteHeaderField(HeaderController.java:60)
    at http_client.ui.main.center_panel.header.single_header.SingleHeaderController.onTapDelete(SingleHeaderController.java:27)
    ... 50 more

做完之后:

Platform.runLater(() -> {
  ....// your code
});

我有:

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:372)
    at java.base/java.util.ArrayList.remove(ArrayList.java:535)
    at javafx.base/com.sun.javafx.collections.ObservableListWrapper.doRemove(ObservableListWrapper.java:116)
    at javafx.base/javafx.collections.ModifiableObservableListBase.remove(ModifiableObservableListBase.java:179)
    at http_client.ui.main.center_panel.header.HeaderController.lambda$deleteHeaderField$0(HeaderController.java:60)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    at java.base/java.lang.Thread.run(Thread.java:834)

第二次編輯:

我有一個名為 list_entries 的 package:

public class ListEntriesController implements Initializable {
    Integer headerCounts = 0;
    private ObservableList<HBox> fields = FXCollections.observableArrayList();

    @FXML
    private JFXListView<HBox> listView;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        addHeaderFiled();
    }

    @FXML
    private void onTapAddNewHeader(MouseEvent mouseEvent) {
        addHeaderFiled();
    }

    @FXML
    private void removeHeaderFiled(MouseEvent mouseEvent) {
        fields.remove(headerCounts - 1);
        headerCounts--;
        listView.setItems(fields);
    }

    private void addHeaderFiled() {
        FXMLLoader entryFiledLoader = new FXMLLoader(getClass().getResource("/http_client/ui/main/center_panel/header/single_header/single_header.fxml"));
        try {
            HBox entryFiled = entryFiledLoader.load();
            fields.add(entryFiled);
            headerCounts++;
            listView.setItems(fields);
            SingleHeaderController controller = headerFiledLoader.getController();
            controller.setHeaderIndex(headerCounts - 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void deleteHeaderField(int index) {
        System.out.println(index);
        Platform.runLater(() -> {
            fields.remove(index);
            headerCounts--;
            listView.setItems(fields);
        });
    }
}

和一個 package 命名條目:

public class EntryController implements Initializable {
    private int entryIndex;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {}

    @FXML
    private void onTapDelete() {
        FXMLLoader EntryLoader = new FXMLLoader(getClass().getResource("/http_client/ui/main/center_panel/header/header.fxml"));
        try {
            EntryLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ListEntriesController controller = headerFiledLoader.getController();
        controller.deleteHeaderField(headerIndex);
    }

    public void setEntryIndex(int index) {
        entryIndex = index;
    }
}

您正在處理兩個獨立的線程。 主 JFX 線程和事件線程。 事件線程(在隊列中)應該與主 JFX 線程同步

Platform.runLater(() -> {
  ....// your code
});

我認為您必須在刪除之前檢查 headerCount

private void removeHeaderFiled(MouseEvent mouseEvent) {
    if (headerCounts > 0) { // avoid negative value
      fields.remove(headerCounts - 1);
      headerCounts--;
      listView.setItems(fields);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM