4 Replies Latest reply on Nov 19, 2012 10:42 PM by 894573

    Having tooltip trouble

      I'm having some tootip issues. The scenario is I'm returning some data from an EJB (which is working). You're supposed hover over a mark on a scatter chart, and it gives you some information about that mark. I can't get it to work. Maybe I'm being too simple about the way I've coded it? Anyway, here's my code:
      ("array" comes from the EJB, and each entry in that array is another array)
              XYChart.Series<Number, Number> normalSeries = new XYChart.Series<>();
              XYChart.Series<Number, Number> negativeOutlierSeries = new XYChart.Series<>();
              XYChart.Series<Number, Number> positiveOutlierSeries = new XYChart.Series<>();
              for (int i = 0; i < array.size(); i++) {
                  ArrayList arr = (ArrayList) array.get(i);
                  // intercept, slope
                  XYChart.Data data = new XYChart.Data(arr.get(0), arr.get(1));
                  System.out.println("tooltip: " + arr.get(3));
                  // tooltip 
                  Tooltip.install(data.getNode(), new Tooltip(arr.get(3).toString()));
                  // check outlier
                  if (Integer.parseInt(arr.get(2).toString()) == 0) {
                  // if the outlier isn't zero, check slope
                  } else if (Float.parseFloat(arr.get(1).toString()) < 0) {
                  } else {
              sc.getData().addAll(normalSeries, negativeOutlierSeries, positiveOutlierSeries);
      The chart displays okay, but when I hover over one of the marks, it doesn't show me anything. I must be doing something wrong, but what?

        • 1. Re: Having tooltip trouble
          I'm guessing, but I think the node property gets set on the data object after the data is added to the chart (maybe even not until the chart becomes part of the scene). This thread Display Tooltips on charts values? has various options for setting tooltips on nodes on chart data.
          • 2. Re: Having tooltip trouble
            Well... I tried doing a little bit of what you mentioned (the link). It still doesn't work, and I'm perplexed.

                    final ScatterChart sc = new ScatterChart(xAxis, yAxis) {
                        protected void layoutPlotChildren() {
                            for (Iterator it = getPlotChildren().iterator(); it.hasNext();) {
                                Object mark = it.next();
                                if (mark instanceof XYChart.Data) {
                                    XYChart.Data data = (XYChart.Data) mark;
                                    Tooltip.install((Node) mark, new Tooltip(data.getExtraValue().toString()));
            (I shoved the tooltip in the extra value.) <sigh> I'm just confused. Any other ideas?

            Edited by: user_891570 on Nov 18, 2012 9:01 PM
            • 3. Re: Having tooltip trouble
              The other mechanism in that thread seems to work ok:
              import java.util.Random;
              import javafx.application.Application;
              import javafx.beans.value.ChangeListener;
              import javafx.beans.value.ObservableValue;
              import javafx.scene.Node;
              import javafx.scene.Scene;
              import javafx.scene.chart.NumberAxis;
              import javafx.scene.chart.ScatterChart;
              import javafx.scene.control.Tooltip;
              import javafx.scene.layout.BorderPane;
              import javafx.stage.Stage;
              import static javafx.scene.chart.XYChart.*;
              public class ScatterChartTest extends Application {
                public void start(Stage primaryStage) throws Exception {
                  BorderPane root = new BorderPane();
                  NumberAxis xaxis = new NumberAxis();
                  NumberAxis yaxis = new NumberAxis();
                  ScatterChart<Number, Number> scatterPlot = new ScatterChart<>(xaxis, yaxis);
                  Series<Number, Number> series = new Series<>();
                  series.setName("Random Data");
                  Random rng = new Random();
                  for (int i=0; i<10; i++) {
                    final double x = rng.nextDouble();
                    final double y = rng.nextDouble();
                    final Data<Number, Number> datum = new Data<Number, Number>(x, y, String.format("x=%.3f%ny=%.3f",x,y));
                    // When node is installed on datum, register a Tooltip on it:
                    datum.nodeProperty().addListener(new ChangeListener<Node>() {
                      public void changed(ObservableValue<? extends Node> observable,
                          Node oldValue, Node newValue) {
                        if (newValue != null) {
                          Tooltip.install(newValue, new Tooltip(datum.getExtraValue().toString()));
                  primaryStage.setScene(new Scene(root, 600, 600));
                public static void main(String[] args) {launch(args);}
              • 4. Re: Having tooltip trouble
                Oh, thank you! That did it. I guess I just don't understand JavaFX too well.