title: Javafx treeview cell factory with gridpane
body: <p>I have a <code>TreeView&lt;GridPane&gt;</code> and the <code>GridPane</code> contains multiple Nodes like Buttons and Labels. The amount of nodes inside the <code>GridPane</code> varies.</p>
  
  <p>I need the cell factory to get the <code>MouseEntered</code> event but when I use the code below then no content gets displayed.</p>
  
  <p>My current code looks like that:</p>
  
  <pre><code>treeView.setCellFactory(tv -&gt; {
      TreeCell&lt;GridPane&gt; cell = new TreeCell&lt;GridPane&gt;() {
          @Override
          protected void updateItem(GridPane item, boolean empty) {
              super.updateItem(item, empty);
          }
      };
  
      cell.setOnMouseEntered(e -&gt; {
          //TODO
      });
  
      return cell ;
  });
  </code></pre>
  
  <p>I know that the <code>updateItem</code> method needs to be modified but I dont know how because the number of nodes inside the <code>GridPane</code> varies.</p>
  
  <p><strong>EDIT</strong> 
  Here are the classes.</p>
  
  <p>TreeController.java:</p>
  
  <pre><code>public class TreeController {
  
      private TreeItem&lt;GridPane&gt; rootItem;
      private int hgap = 5;
  
      @FXML private TreeView&lt;GridPane&gt; treeView;
  
      @FXML
      private void initialize() {
  
          GridPane gp = new GridPane();
          gp.add(new Label("Label1"), 0, 0);
          gp.getColumnConstraints().add(0, new ColumnConstraints());
          gp.getColumnConstraints().get(0).setHgrow(Priority.NEVER);
  
          Button cond = new Button("button");
          gp.add(cond, 1, 0);
          gp.getColumnConstraints().add(1, new ColumnConstraints());
          gp.getColumnConstraints().get(1).setHgrow(Priority.NEVER);
  
  
          gp.add(new Label("Label2"), 2, 0);
          gp.getColumnConstraints().add(2, new ColumnConstraints());
          gp.getColumnConstraints().get(2).setHgrow(Priority.ALWAYS);
  
          Button btnAdd = new Button("Add");
          gp.add(btnAdd, 3, 0);
          gp.setHgap(hgap);
  
          rootItem = new TreeItem&lt;GridPane&gt;(gp);
          rootItem.setExpanded(true);
  
          treeView.setRoot(rootItem);
  
          rootItem.getChildren().add(new TreeItem&lt;GridPane&gt;(createGridRow("Age", false)));
          rootItem.getChildren().add(new TreeItem&lt;GridPane&gt;(createGridRow("Person", true)));
  
          /*
          treeView.setCellFactory(tv -&gt; {
              TreeCell&lt;GridPane&gt; cell = new TreeCell&lt;GridPane&gt;() {
                  @Override
                  protected void updateItem(GridPane item, boolean empty) {
                      super.updateItem(item, empty);
                  }
              };
  
              cell.setOnMouseEntered(e -&gt; {
                  System.out.println("changed cell");
              });
  
              return cell ;
          });
          */
      }
  
      private GridPane createGridRow(String selectedCol, Boolean showAddButton) {
          int index = 0;
          GridPane gp = new GridPane();
  
          Button columnName = new Button(selectedCol);
          gp.add(columnName, index, 0);
          gp.getColumnConstraints().add(index, new ColumnConstraints());
          gp.getColumnConstraints().get(index).setHgrow(Priority.NEVER);
          index++;
  
          Button condition = new Button("greater than");
          gp.add(condition, index, 0);
          gp.getColumnConstraints().add(index, new ColumnConstraints());
          gp.getColumnConstraints().get(index).setHgrow(Priority.NEVER);
          index++;
  
  
          Button value = new Button("[enter value]");
          gp.add(value, index, 0);
          gp.getColumnConstraints().add(index, new ColumnConstraints());
          gp.getColumnConstraints().get(index).setHgrow(Priority.ALWAYS);
          index++;
  
          if (showAddButton) {
              Button btnAdd = new Button("Add");
              gp.add(btnAdd, index, 0);
              gp.getColumnConstraints().add(index, new ColumnConstraints());
              gp.getColumnConstraints().get(index).setHgrow(Priority.NEVER);
              index++;
          }
  
          gp.setHgap(hgap);
  
          return gp;
      }
  
  }
  </code></pre>
  
  <p>MainClass.java:</p>
  
  <pre><code>public class MainClass extends Application{
  
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage stage) {
          Parent root;
          try {
              TreeController controller = new TreeController();
  
              FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TreeController.fxml"));
              fxmlLoader.setController(controller);
              root = fxmlLoader.load();
  
              Scene scene = new Scene(root, 600, 500);
  
              stage.setMinHeight(400);
              stage.setMinWidth(500);
              stage.setTitle("TreeController");
              stage.setScene(scene);
              stage.setResizable(true);
              stage.show();
          } catch (IOException e) {
              e.printStackTrace();
          };    
      }
  
      @Override
      public void stop() {
          System.exit(0);
      }
  
  }
  </code></pre>
  
  <p>TreeController.fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.ScrollPane?&gt;
  &lt;?import javafx.scene.control.TreeView?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  
  &lt;AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"     prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.161" xmlns:fx="http://javafx.com/fxml/1"&gt;
     &lt;children&gt;
        &lt;ScrollPane fitToHeight="true" fitToWidth="true" layoutX="155.0" layoutY="111.0" prefHeight="200.0" prefWidth="200.0"     AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0"&gt;
           &lt;content&gt;
              &lt;TreeView fx:id="treeView" prefHeight="200.0" prefWidth="200.0" /&gt;
           &lt;/content&gt;
        &lt;/ScrollPane&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>