gpt4 book ai didi

javafx-8 - Javafx TableView 清除选择给出 NullPointerException

转载 作者:行者123 更新时间:2023-12-03 22:48:18 25 4
gpt4 key购买 nike

我在清除 JavaFX TableView 的选择时遇到了一些问题。
当我运行 myTableView.getSelectionModel().clearSelection() 时出现以下错误。

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.handleSelectedCellsListChangeEvent(TableView.java:2657)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.clearAndSelect(TableView.java:2180)
at javafx.scene.control.TableView$TableViewSelectionModel.clearAndSelect(TableView.java:1733)
at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.simpleSelect(TableCellBehaviorBase.java:318)
at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.doSelect(TableCellBehaviorBase.java:231)
at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.mousePressed(TableCellBehaviorBase.java:144)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
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.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:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.access$200(GtkApplication.java:48)
at com.sun.glass.ui.gtk.GtkApplication$6$1.run(GtkApplication.java:149)
at java.lang.Thread.run(Thread.java:744)

有没有人有这种错误?
我为 TableView 和我的内容进行了绑定(bind)。
TableView 显示我想要的内容。我想从表格 View 中选择内容并处理内容,然后想从表格 View 中删除内容。

我的表选择模型监听器如下:
orderTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {

@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
setIndex(orderTable.getSelectionModel().getSelectedIndex());
}
});

索引是一个整数属性。我在整数属性中保留索引并监听变化。
index.addListener(new ChangeListener() {

@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
Integer newVal = (Integer) newValue;
System.out.println(String.format("Selected index changed: %s", newVal));

orderTable.getSelectionModel().clearSelection();
}
});

这里的清除部分给了我上面的错误。需要一些帮助吗?
谢谢

最佳答案

我有完全相同的错误。我相信这是因为您在 ChangeListener 内部时正在修改选定的索引。对于选定的索引。如果您查看堆栈跟踪,看起来您正在遇到事件冒泡循环。所以基本上,你需要让 ChangeListener在再次修改所选索引之前退出并让用于处理所选索引的其他事件发生变化。为了回应Zoltan的第一个答案,我也有这个想法,但是简单的调试表明自定义ChangeListener没有被多次调用。

就我而言,我在 ChangeListener 中打开了一个对话框。然后在对话框关闭时尝试清除索引。对我来说,包装 dialog.show() Platform.runLater() 中的方法方法足以让所有事件首先得到处理。

您可能想尝试将您的电话转至 clearSelection()Platform.runLater()方法。例如

Platform.runLater( ()-> {  orderTable.getSelectionModel().clearSelection();  });

不确定它是否会起作用,但值得一试。
  • chooks
  • 关于javafx-8 - Javafx TableView 清除选择给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23098483/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com