Hamon148
Hamon148

Reputation: 147

JavaFX putting Image into a TableView using CellFactory

As the title says, I need to insert an Image into a column of a TableView by setting a CellFactory but the image, whose path is opbtained via FileChooser, doesn't show up on the table. However, if I select the first row it is highlighted as if there was something there. Here is the snippet of my code from my controller class:

@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"));
    }

I'm not sure what is going wrong, or what to try in order to fix this. Any help is appreciated!

Upvotes: 1

Views: 2470

Answers (1)

fireandfuel
fireandfuel

Reputation: 732

Make sure the paths of the images are correct. Otherwise the image cells will not show a picture (and surprisingly will not throw an exception).

PS: I've found a slightly modified version of your code (perhaps one of your sources): TableView Cell Modify in JavaFX.

Upvotes: 1

Related Questions