从ListView中删除项目时出错

时间:2021-06-06 17:02:07

I have two ListViews, each being populated by a separate ArrayList. I'm trying to make it so that in left ListView, double clicking an item copies it to the right, and double clicking in the right removes the item. Whenever I try to add an item, I get a whole slew of errors, and nothing changes. Removing items works about half the time; It's really weird, sometimes when I run it things will remove just fine, and other times, again, I just get a whole host of errors. Some code:

我有两个ListView,每个都由一个单独的ArrayList填充。我正在尝试这样做,以便在左侧ListView中,双击项目将其复制到右侧,然后双击右侧将删除该项目。每当我尝试添加一个项目时,我都会遇到一大堆错误,而且没有任何变化。删除项目的工作时间大约是一半;这真的很奇怪,有时当我运行时,事情会很好地删除,有时候,再次,我只是得到了一大堆错误。一些代码:

//Right list view
    ListView<String> lv_right = new ListView<>
    (FXCollections.observableArrayList(selectedSpells));
    lv_right.setPrefSize(175, 598);
    lv_right.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);


    //Double clicking in right list
    lv_right.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2 &&
               (event.getTarget() instanceof Labeled || ((BorderPane) event.getTarget()).getChildren().size() > 0)) {

                //Removing items from right list
                String item = lv_right.getSelectionModel().getSelectedItem();
                lv_right.getItems().remove(item);
             }    
        }
    });

I have the exact same thing right now for the left ListView, with the exception of lv_left.getItems().remove(item); being replaced with cleric_cantrips.add(item); which never works at all.

我现在对左侧ListView完全相同,但lv_left.getItems()除外.remove(item);被替换为cleric_cantrips.add(item);这根本不起作用。

The slew of errors:

一大堆错误:

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: com.sun.javafx.scene.control.skin.LabeledText cannot be cast to javafx.scene.layout.BorderPane
at spell_manager$1.handle(spell_manager.java:52)
at spell_manager$1.handle(spell_manager.java:1)
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.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$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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(Unknown Source)

Normally junk like this means that there's a problem with the code itself, but I've no errors there, so I figured I'd add this here in case anyone wanted to see it rather than wait for someone to ask if they did need it.

通常像这样的垃圾意味着代码本身存在问题,但我没有错误,所以我想我会在这里添加它以防万一有人想看到它而不是等待有人问他们是否确实需要它。

I'm honestly at a loss for what to do. I'm still new to using JavaFX...thanks in advance guys!

老实说我不知道​​该怎么办。我还是刚开始使用JavaFX ...先谢谢你们!

1 个解决方案

#1


0  

Beacause when you used || ,the target can not be labeled and borderPane in the same time,for example when you make double click to labeled, the target here is labeled ,after that we move to other choice you tried to cast labeled to borderPane so you need to write each condition alone like this

因为你使用了|| ,目标不能同时标记和borderPane,例如当你双击标记时,这里的目标被标记,之后我们移动到你试图强制标记为borderPane的其他选择,所以你需要写每个像这样的条件

 lv_right.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2)
           {
             if(event.getTarget() instanceof Labeled) {

            //Removing items from right list
            String item = lv_right.getSelectionModel().getSelectedItem();
            lv_right.getItems().remove(item);
         }   
            if(event.getTarget() instanceof BorderPane) {
             BorderPane border=(BorderPane)event.getTarget;
            if(border.getChildren().size>0)
             {
            //Removing items from right list
            String item = lv_right.getSelectionModel().getSelectedItem();
            lv_right.getItems().remove(item);
           }
           }    
    }
});

#1


0  

Beacause when you used || ,the target can not be labeled and borderPane in the same time,for example when you make double click to labeled, the target here is labeled ,after that we move to other choice you tried to cast labeled to borderPane so you need to write each condition alone like this

因为你使用了|| ,目标不能同时标记和borderPane,例如当你双击标记时,这里的目标被标记,之后我们移动到你试图强制标记为borderPane的其他选择,所以你需要写每个像这样的条件

 lv_right.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2)
           {
             if(event.getTarget() instanceof Labeled) {

            //Removing items from right list
            String item = lv_right.getSelectionModel().getSelectedItem();
            lv_right.getItems().remove(item);
         }   
            if(event.getTarget() instanceof BorderPane) {
             BorderPane border=(BorderPane)event.getTarget;
            if(border.getChildren().size>0)
             {
            //Removing items from right list
            String item = lv_right.getSelectionModel().getSelectedItem();
            lv_right.getItems().remove(item);
           }
           }    
    }
});