java - XYChart.Data getNode() 为空

标签 java javafx

我尝试获取 XYChart.Data 的节点,但它为空。

var a = new XYChart.Data<Number, Number>(10, 10);

if(a.getNode()==null){ System.out.println("true"); }

为什么 getNode() 返回 null?我做错了什么?

最佳答案

XYChart.Data.node 属性的初始值为 null。如果您不自己设置它,那么一旦数据成为图表的一部分,它就会被设置。来自 the documentation (强调我的):

The node to display for this data item. You can either create your own node and set it on the data item before you add the item to the chart. Otherwise the chart will create a node for you that has the default representation for the chart type. This node will be set as soon as the data is added to the chart. You can then get it to add mouse listeners etc. Charts will do their best to position and size the node appropriately, for example on a Line or Scatter chart this node will be positioned centered on the data values position. For a bar chart this is positioned and resized as the bar for this data item.

如果您需要对默认的 Node 做一些事情,您可以向该属性添加一个监听器(在将数据添加到图表之前),以便在它变为非 时收到通知空

data.nodeProperty().addListener((observable, oldValue, newValue) -> {
    if (newValue != null) {
        // do something...
    }
});

或者在将数据添加到图表后获取节点

关于java - XYChart.Data getNode() 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394315/

相关文章:

java - 使用路径变量进行 GET 映射的自定义验证

java - 将 JSON 响应映射到多个 POJO

JavaFX - 生成 App Jar 时不显示虚拟键盘

java - 每 1 秒为图像指定一个新的 X 和 Y 位置

java - 从 JavaFX 打印时设置要使用的 CSS

java - 如何构建JPA实体和JSON模型?

Java反模式名称?包含对象的对象包含...等等

java - 如何增加 ToggleGroup 中可能选择的数量(例如 RadioButtons)?

java - 如何从JavaFX中的二级线程获取更新消息/属性?

java - GridBagLayout - 向约束添加边距以将组件从中心移动到顶部