I'm playing around with JavaFX and wanted to add a tooltip that pops up with the data value when the mouse is hovering over the node. I found several links and answers out there describing CSS methods for doing it, or using Tooltip.install(node, tooltip) and while I could get a tooltip working on a dummy example, I was still having no luck with the chart, using code like so :
LineChart<Number, Number> chart = new LineChart(xaxis, yaxis);
ObservableList<Data<Number, Number>> data = FXCollections.observableArrayList();
Data<Number, Number> d1 = new XYChart.Data<Number, Number>(5, 15);
Tooltip tooltip = new Tooltip("15");
Tooltip.install(d1.getNode(), tooltip);
data.add(d1);
Data<Number, Number> d2 = new XYChart.Data<Number, Number>(10, 25);
Tooltip tooltip2 = new Tooltip("25");
Tooltip.install(d2.getNode(), tooltip2);
data.add(d2);
chart.setData(data);
//add chart to scene etc etc etc
Answers
After some digging, the issue here is that a data element (XYChart.Data) does not have a node created at construction time. chart.setData(data) will populate the node field - I believe this is to allow the user to create and set their own nodes if so desired. So d1.getNode() is actually returning null, and Tooltip.install() is silently failing.
Moving the Tooltip.install call after chart.setData solves the issue.
コメント