我尝试获取 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/