title: JavaFX PiChart, my hover values blink
body: <h2>Context :</h2>
  
  <p>Hi ! 
  I'm trying to create a little popup which display the value of slice when mouse hover, on my <strong>PieChart</strong> (with JavaFX).</p>
  
  <p>I successed on my <strong>LineChart</strong>, <strong>AreaChart</strong> etc.. Thanks this post : <a href="https://stackoverflow.com/questions/14615590/javafx-linechart-hover-values">JavaFX LineChart Hover Values</a> (thank you so much Jewelsea for your help).</p>
  
  <h2>Problem (1/2) :</h2>
  
  <p>But with the <strong>PieChart</strong>, I have a problem : The popup is blinking oO </p>
  
  <p><img src="https://i.stack.imgur.com/vMvQZ.gif" alt="enter image description here"></p>
  
  <h2>My code :</h2>
  
  <p>With syntactic color : <a href="https://bpaste.net/show/12838ad6b2e2" rel="nofollow noreferrer">https://bpaste.net/show/12838ad6b2e2</a></p>
  
  <pre><code>import java.util.ArrayList;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.concurrent.Task;
  import javafx.event.EventHandler;
  import javafx.scene.Cursor;
  import javafx.scene.Group;
  import javafx.scene.Node;
  import javafx.scene.chart.PieChart;
  import javafx.scene.control.Label;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.StackPane;
  import com.alpha.client.view.nodes.stats.statsEngine.beans.ListRepere;
  import com.alpha.client.view.nodes.stats.statsEngine.beans.OptionsChart;
  import com.alpha.client.view.nodes.stats.statsEngine.beans.ValueStat;
  
  /**
   *
   * @author Zombkey
   */
  public class PieChartNode implements ChartNode {
  
  //My personnal attributes
  private ListRepere categories;
  private ArrayList&lt;ValueStat&gt; values;
  //The PieChart
  private PieChart chart;
  
  //The data of Chart, will be fill by a thread
  private ObservableList&lt;PieChart.Data&gt; pieChartData;
  //The node which contain chart and label
  private Group group;
  //The Label 
  private final Label caption;
  
  public PieChartNode(ListRepere categories, ArrayList&lt;ValueStat&gt; values, OptionsChart optionsChart) {
      this.categories = categories;
      this.values = values;
  
      //New Group
      group = new Group();
      //I must use a StackPane to place Label hover Chart
      StackPane pane = new StackPane();
      group.getChildren().add(pane);
  
      //Init' PieChart
      pieChartData = FXCollections.observableArrayList();
      chart = new PieChart(pieChartData);
      chart.setStartAngle(180.0);
      //Add chart to StackPane
      pane.getChildren().add(chart);
  
      //Init Popup(Label)
      caption = new Label("");
      caption.setVisible(false);
      caption.getStyleClass().addAll("chart-line-symbol", "chart-series-line");
      caption.setStyle("-fx-font-size: 12; -fx-font-weight: bold;");
      caption.setMinSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE);
      //Add Label to StackPane
      pane.getChildren().add(caption);
  }
  
  @Override
  public Node getNodeGraph() {
      return (Node) group;
  }
  
  @Override
  public Task initTaskFormat() {
      Task&lt;Void&gt; task = new Task&lt;Void&gt;() {
          @Override
          protected Void call() throws Exception {
              //i and sizeOfallElements are just use for ProgressBar 
              int i = 0;
              int sizeOfallElements = values.size();
              updateProgress(i, sizeOfallElements);
  
              //For Each ValueStat (a Personnal pojo Class), I must create a slice 
              for (ValueStat v : values) {
                  //Create the PieChart.Data and add it to ObservableList
                  PieChart.Data dataTemp = new PieChart.Data(v.getCategorie().getStringName(), v.getDoubleValue());
                  pieChartData.add(dataTemp);
  
                  //HERE, the interessante code !
                  //At the same way that the LineChart, I add Event when mouse entered and mouse exited.
                  //When mouse entered (on the slice of PieChart)
                  dataTemp.getNode().addEventHandler(MouseEvent.MOUSE_ENTERED,
                          new EventHandler&lt;MouseEvent&gt;() {
                              @Override
                              public void handle(MouseEvent e) {
                                  System.out.println("MOUSE_ENTERED : "+dataTemp.getName());
                                  //I display Label
                                  caption.setVisible(true);
                                  //I move Label near the mouse cursor
                                  caption.setTranslateX(e.getX());
                                  caption.setTranslateY(e.getY());
                                  //I hide the mouse cursor
                                  dataTemp.getNode().setCursor(Cursor.NONE);
                                  //I change text of Label
                                  caption.setText(String.valueOf(dataTemp.getPieValue()) + "\n" + dataTemp.getName());
                                  //I try to change the frame color of Label
                                  caption.getStyleClass().add(dataTemp.getNode().getStyleClass().get(2));
                              }
                          });
  
                  //When mouse exited (the slice of PieChart)
                  dataTemp.getNode().addEventHandler(MouseEvent.MOUSE_EXITED,
                          new EventHandler&lt;MouseEvent&gt;() {
                              @Override
                              public void handle(MouseEvent e) {
                                  System.out.println("MOUSE_EXITED : "+dataTemp.getName());
                                  //I Hide Label
                                  caption.setVisible(false);
                                  //I show the mouse cursor
                                  dataTemp.getNode().setCursor(Cursor.DEFAULT);
                              }
                          });
                  //Update progress
                  updateProgress(i++, sizeOfallElements);
              }
              return null;
          }
      };
      return task;
  }
  }
  </code></pre>
  
  <h2>Problem (2/2) :</h2>
  
  <p>The problem is that the events (MOUSE_ENTERED and MOUSE_EXITED) are emitted, too often instead of once.</p>
  
  <p><em>Ex :</em>
  I just put in, then put off, my mouse hover a slice.
  Here the result on console : </p>
  
  <pre><code>MOUSE_ENTERED : BC
  MOUSE_EXITED : BC
  MOUSE_ENTERED : BC
  MOUSE_EXITED : BC
  MOUSE_ENTERED : BC
  MOUSE_EXITED : BC
  MOUSE_ENTERED : BC
  MOUSE_EXITED : BC
  </code></pre>
  
  <p>Anyone know why the event bug ? </p>
  
  <p>Thanks : )</p>