如何从FilteredList中删除项目?

时间:2014-09-17 13:50:36

标签: java javafx

我正在使用JavaFX 8.我尝试过滤一个TableView并且我这样做了,但我还有一些其他功能,比如修改和删除表中的项目。添加过滤后,修改工作正常但删除不受支持!! 这是我实施的:

public void setMainApp(MainApp mainApp) {
    this.mainApp = mainApp;
    refreshCategories();

    FilteredList<Product> filteredProducts = new FilteredList<>(mainApp.getProductData(), p -> true);

    categoryCombo.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
        filteredProducts.setPredicate(product -> {

            if (newValue == null || newValue.isEmpty() || newValue.equals("None"))
                return true;
            String lowerCaseFilter = newValue.toLowerCase();
            if (product.getCategory().toLowerCase().indexOf(lowerCaseFilter) == -1) {
                return false;
            }
            return true;
        });
    });


    productTable.setItems(filteredProducts);

}

这是删除实现:

@FXML
private void handleDelete() {
    int selectedIndex = productTable.getSelectionModel().getSelectedIndex();
    if (selectedIndex >= 0) {
        Action responce = Dialogs.create().title("Supprission").masthead("Supprission definitive")
                .message("Êtes-vous sûre de vouloir supprimer ce produit?").actions(Dialog.Actions.OK, Dialog.Actions.CANCEL).showConfirm();
        if (responce == Dialog.Actions.OK) {
            Product product = productTable.getSelectionModel().getSelectedItem();
            dao.removeProductByEntity(product);
            productTable.getItems().remove(selectedIndex);
        }
    } else {
        Dialogs.create().title("Pas de selection").masthead("Pas de produit Selectionné").message("S'il vous plait, selectionné un produit dans la liste.")
                .showWarning();
    }
}

这是导致异常的行:

productTable.getItems().remove(selectedIndex);

最后这是Exception消息:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    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.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8216)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    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.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
    at com.sun.glass.ui.View.notifyMouse(View.java:925)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/584634336.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: 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:483)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1759)
    ... 51 more
Caused by: java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(AbstractList.java:161)
    at dz.bilaldjago.homekode.controller.ProductOverviewController.handleDelete(ProductOverviewController.java:103)
    ... 61 more

我的主要问题是:ReadOnly列表是否可能接受修改而不是删除! 我希望这一点清晰明白。

2 个答案:

答案 0 :(得分:4)

我知道这是一个迟到的答案,但也许有人会发现它很有用。我今天偶然发现了这个问题。我正在开发一个应用程序,它将成为一个小型商店的简单仓库经理。主要部分是TableView组件,可以通过添加,删除或过滤项目来动态更新。

实际上,您不能向FilteredList添加od删除项,因为它会抛出UnsupportedOperationException,即使对于操作本身,您将使用基本的ObservableList包装它。可以做的是保持TableView项的单独ObservableList和FilteredList,如果需要更新,只更新第一个,而不是将其包装在FilteredList中,然后才将其用作TableView.setItems()方法的源。 / p>

演示代码:

public class DynamicFilteredList {
  @FXML
  private TableView tableView;
  @FXML
  private TextField searchField;
  private ObservableList tableItems;
  private FilteredList filteredTableItems;

  private void wrapListAndAddFiltering() {
        //Wrap ObservableList into FilteredList
        filteredTableItems = new FilteredList<>(tableItems, p -> true);

        //Add change listener to searchField component to filter the table view
        searchField.textProperty().addListener((observable, oldValue, newValue) -> {
            filteredTableItems.setPredicate(item -> {
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }
                String query = newValue.toLowerCase();
                if (item.getName().toLowerCase().contains(query)) {
                    return true;
                }
                return false;
            });
        });
    }

    private void add(Item item) {
        tableItems.add(item);
        wrapListAndAddFiltering();
        tableView.setItems(filteredTableItems);
    }

    private void remove(Item item) {
        tableItems.remove(item);
        wrapListAndAddFiltering();
        tableView.setItems(filteredTableItems);
    }


}

我在这种方法中看到的主要缺点是,对于非常大的列表,它可能是资源(尤其是内存)消耗,并且可能对应用程序的整体性能产生负面影响。

答案 1 :(得分:0)

我遇到了同样的问题,我发现可以使用

访问基础列表
myFilteredList.getSource() 

就我而言,我使用的是ObservableList,并且能够使用

从“源列表”中删除项目
myFilteredList.getSource().remove(itemReference);

我希望这会有所帮助。