2014-07-21 44 views
0

所以我有這TreeTableView,我可以讓文本正常顯示,但按鈕和圖像不想顯示。我目前正在使用重寫TreeTableCell類及其更新方法的方法。JavaFX 8 - 添加組件到TreeTable單元

TreeTableColumn<Application, Application> installed = new TreeTableColumn<Application, Application>("Installed?"); 
    installed.setResizable(false); 
    installed.setPrefWidth(308.9); 
    installed.setCellFactory(new Callback<TreeTableColumn<Application, Application>, TreeTableCell<Application, Application>>() 
    { 
     @Override 
     public TreeTableCell<Application, Application> call(TreeTableColumn<Application, Application> param) 
     { 
      TreeTableCell<Application, Application> cell = new TreeTableCell<Application, Application>() 
      {     
       @Override 
       public void updateItem(Application app, boolean empty) 
       { 
        if(app != null) 
        { 
         setGraphic(app.getInstalledImage()); 
        } 
       } 
      };    
      return cell; 
     } 

    }); 
    appTree.getColumns().add(installed); 

但是,「應用程序」拋出一個NullPointerException異常時不檢查應用程序!= NULL,所以我想,可以解決我的問題。任何想法爲什麼一個有效的應用程序沒有通過?

回答

1

TreeTableView包含空單元;單元格位於最後一個填充行下方的空間中,也可能是某些列中的單元格用於摺疊行。空單元格始終有updateItem(...)null項目調用。

注意,它總是必不可少的調用super.updateItem(...)在您的實現:

  @Override 
      public void updateItem(Application app, boolean empty) 
      { 
       super.updateItem(app, empty); 
       if(app != null) 
       { 
        setGraphic(app.getInstalledImage()); 
       } 
      } 

有關詳細信息,請參閱Javadocs for Cell