I try to get node of an XYChart.Data
, but it is null.
var a = new XYChart.Data<Number, Number>(10, 10);
if(a.getNode()==null){ System.out.println("true"); }
Why getNode()
return null? What I do wrong?
The XYChart.Data.node
property has an initial value of null
. If you don't set it yourself then it will be set once the data becomes part of a chart. From the documentation (emphasis mine):
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.
If you need to do something with the default Node
, you can add a listener to the property (before adding the data to a chart) to be notified of when it becomes non-null
.
data.nodeProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
// do something...
}
});
Or get the Node
after you've added the data to a chart.