2016-11-21 31 views
1

正如標題所說,我需要通過設置CellFactory將圖像插入到TableView的列中,但圖像(其路徑通過FileChooser運行)不會顯示在表。但是,如果我選擇第一行,它會突出顯示,好像有什麼東西。 這裏是我的代碼從我的控制器類的代碼片段:JavaFX使用CellFactory將圖像放入TableView中

@FXML 
private void initialize() 
{ 
    TableColumn thumbNail = new TableColumn("Photo"); 
    thumbNail.setCellValueFactory(new PropertyValueFactory("image")); 
    thumbNail.setPrefWidth(200); 

    thumbNail.setCellFactory(new Callback<TableColumn<Image, Photo>, TableCell>() 
    { 
     @Override 
     public TableCell<Image, Photo> call(TableColumn<Image, Photo> param) 
     { 
      TableCell<Image, Photo> cell = new TableCell<Image, Photo>() 
      { 
       ImageView img = new ImageView(); 
       @Override 
       public void updateItem(Photo item, boolean empty) 
       { 
        if(item != null) 
        { 
         HBox box = new HBox(); 
         box.setSpacing(10); 
         VBox vbox = new VBox(); 
         vbox.getChildren().add(new Label("THIS")); 
         vbox.getChildren().add(new Label("DATE")); 

         img.setFitHeight(50); 
         img.setFitWidth(50); 
         img.setImage(new Image(new File(item.getPath()).toURI().toString())); 

         box.getChildren().addAll(img, vbox); 
         setGraphic(box); 
        } 
       } 
      }; 
      return cell; 
     } 
    }); 
    photoView.getColumns().addAll(thumbNail); 
    loadScreen(); 
} 

// Load the album contents. 
public void loadScreen() 
{ 
    if(imgList != null) 
    { 
     imgList.clear(); 
    } 
    if(currUser != null && thisAlbum.getPhotoList() != null) 
    { 
     imgList = FXCollections.observableArrayList(thisAlbum.getPhotoList()); 
     //FXCollections.sort(obsList); 

     photoView.setItems(imgList); 
    } 
    if(imgList == null) 
    { 
     photoView.setPlaceholder(new Label("No Photos In List")); 
    } 

我不知道什麼錯誤,或者是爲了解決這個嘗試。任何幫助表示讚賞!

回答

1

確保圖像路徑正確。否則圖像單元不會顯示圖片(並且令人驚訝的是不會拋出異常)。 PS:我找到了一個稍微修改過的代碼版本(可能是您的源代碼): TableView Cell Modify in JavaFX

+0

路徑是正確的,我將它們打印到控制檯進行檢查。這個鏈接實際上是我幫助編寫代碼的地方。 – Hamon148

+0

@ Hamon148我是sry,我無法重現爲什麼圖像沒有加載。你可以將整個項目上傳到github或文件託管平臺,或者你可以寫一個SSCCE嗎? – fireandfuel