我有两个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);
}
}
});
我现在对左侧ListView有完全相同的事情,但lv_left.getItems().remove(item);
被cleric_cantrips.add(item);
替换,但它根本不起作用。
一大堆错误:
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)
通常这样的垃圾意味着代码本身存在问题,但我没有错误,所以我想我会在这里添加这个以防万一有人想看到它而不是等待有人问他们是否确实需要它。
老实说我不知道该怎么做。我还是刚开始使用JavaFX ...先谢谢你们!
答案 0 :(得分:0)
因使用||
而导致目标无法同时标记为和 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);
}
}
}
});