1 Reply Latest reply on Sep 16, 2013 10:44 PM by James_D

    how to assign treecell just for some treeitems

    de373f44-3503-413f-93f9-db70db6c30ce

      Hi

      i got this simple code

          

      TreeView<String> tree = new TreeView<String>();
          
              TreeItem<String> root = new TreeItem<String>("root");
              TreeItem<String> computer = new TreeItem<String>("computer");
              TreeItem<String> inbox = new TreeItem<String>("inbox");
              TreeItem<String> sent = new TreeItem<String>("sent");
              computer.getChildren().addAll(inbox,sent);
              computer.setExpanded(true);
              root.getChildren().addAll(computer);
              root.setExpanded(true);
          
              tree.setRoot(root);
          
          
              tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
              
                  @Override
                  public TreeCell<String> call(TreeView<String> param) {
                  
                      return new TreeCell<String>(){
                      
                      
                      
                      
                          @Override
                          public void updateItem(String item,boolean empty){
      
                              super.updateItem(item,empty);
      
                              System.out.println(getTreeItem());
                          
                          
                              if(!empty){
      
                                  setText(item.toString());
      
                              }
      
                          
                          
                          }
                      
                      
                      
                      };
                  }
              });
          
          
              Scene scene = new Scene(tree);
              primaryStage.setScene(scene);
              primaryStage.sizeToScene();
              primaryStage.show();
      

       

       

         

               I dont unterstand why this is the output

       

      TreeItem [ value: root ]

      null

      TreeItem [ value: root ]

      TreeItem [ value: computer ]

      TreeItem [ value: inbox ]

      TreeItem [ value: sent ]

      null

      null

      null

      null

      null

      null

      null

      null

      null

      null

      null

      null

      null

      null

      null

       

               i created 4 treeitems and i wanted assign treecell to it. why did i get nulls a twice root treeitem

       

           thank you

        • 1. Re: how to assign treecell just for some treeitems
          James_D
          why did i get nulls

          The nulls are probably calls to updateItem(...) on empty cells (cells that are out of the range of the number of items currently displayed in the tree). You could check by displaying the value of the "empty" flag.


          twice root treeitem

          Not sure, but there are really no guarantees as to when or how often the updateItem(...) method will be called. Cells can be reused (e.g. if you scroll, a cell for an item that is no longer displayed might (or might not) be reused to display another item). On the other hand, a new cell could be assigned to an already displayed item if there is reason to do it. The details just depend on the implementation of the TreeView and may change from release to release.


          If it's important to you that this happens, you're probably not using the cells as they were intended to be used.