title: How to initialize a treeview control defined in JavaFX fxml file by code
body: <p>The effect I want to achieve is as follows, all created by code.
  <a href="https://i.stack.imgur.com/k1hu6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k1hu6.png" alt="enter image description here"></a></p>
  
  <p>The code is as follows，This method can achieve the result I want because in the start method, first initialize treeview, then create a scene, add it in, and then display it successfully. Example1：</p>
  
  <pre><code>public class treeViewDemo_4 extends Application {
  
      // Node图标
      private final Node rootIcon = new ImageView(
          new Image(getClass().getResourceAsStream("/icon/file.png"))
      );
  
      private TreeView&lt;String&gt; createTreeView(TreeItem&lt;String&gt; root1,TreeItem&lt;String&gt; root2) {
          TreeItem&lt;String&gt; dummyRoot = new TreeItem&lt;&gt;();
          dummyRoot.getChildren().addAll(root1,root2);
          TreeView&lt;String&gt; tree = new TreeView&lt;&gt;(dummyRoot);
          tree.setShowRoot(false);
          return tree ;
      }
  
      final TreeItem&lt;String&gt; root1 = new TreeItem&lt;&gt;("root 1");
      final TreeItem&lt;String&gt; root2 = new TreeItem&lt;&gt;("root 2");
  
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage primaryStage) {
          primaryStage.setTitle("Tree View Sample");
          // 每个Item下又可以添加新的Item
          for (int i = 1; i &lt; 6; i++) {
              TreeItem&lt;String&gt; item = new TreeItem&lt;&gt; ("Message" + i);
              item.getChildren().add(new TreeItem&lt;String&gt;("第三级"));
              root1.getChildren().add(item);
          }
          // 创建TreeView
          TreeView&lt;String&gt; tree = createTreeView(root1,root2);
  
          StackPane root = new StackPane();
          root.getChildren().add(tree);
          primaryStage.setScene(new Scene(root, 300, 250));
          primaryStage.show();
      }
  } 
  
  </code></pre>
  
  <p>But I want to achieve this effect through the fxml file, but I don't know how. First of all, my code, Example2：</p>
  
  <p>fxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.TreeView?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane fx:controller="com.fendo.flying.demo.treeview.demo5.demo5Controller" fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="705.0" prefWidth="1113.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" &gt;
     &lt;children&gt;
         &lt;TreeView fx:id="treeView" layoutX="3.0" layoutY="94.0" prefHeight="608.0" prefWidth="214.0"&gt;
  
         &lt;/TreeView&gt;
        &lt;Button layoutX="300.0" layoutY="93.0" mnemonicParsing="false" text="Button" /&gt;
        &lt;Button layoutX="715.0" layoutY="93.0" mnemonicParsing="false" text="Button" /&gt;
        &lt;Button layoutX="597.0" layoutY="93.0" mnemonicParsing="false" text="Button" /&gt;
        &lt;Button layoutX="393.0" layoutY="93.0" mnemonicParsing="false" text="Button" /&gt;
        &lt;Button layoutX="492.0" layoutY="93.0" mnemonicParsing="false" text="Button" /&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  
  </code></pre>
  
  <p>controller</p>
  
  <pre><code>
  public class demo5Controller implements Initializable {
  
      @FXML
      private TreeView treeView;
  
      final TreeItem&lt;String&gt; root1 = new TreeItem&lt;&gt;("root 1");
      final TreeItem&lt;String&gt; root2 = new TreeItem&lt;&gt;("root 2");
      final TreeView&lt;String&gt; tree = createTreeView(root1,root2);
  
      private TreeView&lt;String&gt; createTreeView(TreeItem&lt;String&gt; root1,TreeItem&lt;String&gt; root2) {
          TreeItem&lt;String&gt; dummyRoot = new TreeItem&lt;&gt;();
          dummyRoot.getChildren().addAll(root1,root2);
          TreeView&lt;String&gt; tree = new TreeView&lt;&gt;(dummyRoot);
          tree.setShowRoot(false);
          return tree ;
      }
  
  
      public demo5Controller() {
          treeView = createTreeView(root1,root2);
      }
  
      @Override
      public void initialize(URL location, ResourceBundle resources) {
          treeView = createTreeView(root1,root2);
      }
  }
  
  </code></pre>
  
  <p>Example2 Invalid after run. Treeview did not initialize successfully, display is empty ,What I want to know is how to initialize treeview defined in fxml through code?</p>
  
  <p>Or, I defined a treeview in fxml. How to initialize or overwrite it in controller? To achieve the effect in example 1</p>
  
  <p>How to make the implementation in example 2, that is, the effect of the following code in example 1:</p>
  
  <pre><code>// create TreeView
  TreeView&lt;String&gt; tree = createTreeView(root1,root2);
  
  StackPane root = new StackPane();
  root.getChildren().add(tree);
  primaryStage.setScene(new Scene(root, 300, 250));
  primaryStage.show();
  
  </code></pre>