Для этого вам нужен пользовательский cellFactory
.Кроме того, я рекомендую использовать массив Image[]
в RecordInfo
вместо:
public class RecordInfo {
private Image[] pic;
public Image[] getPic() {
return pic;
}
public void setPic(Image[] pic) {
this.pic = pic;
}
}
public class ImagesTableCell<T> extends TableCell<T, Image[]> {
private final List<ImageView> imageViews;
private final HBox container;
private ImagesTableCell(List<ImageView> imageViews) {
this.container = new HBox();
this.imageViews = imageViews;
}
private ImageView getOrCreateImageView() {
if (!imageViews.isEmpty()) {
return imageViews.remove(imageViews.size() - 1);
} else {
ImageView iv = new ImageView();
iv.setFitHeight(20);
iv.setFitWidth(20);
return iv;
}
}
@Override
protected void updateItem(Image[] item, boolean empty) {
super.updateItem(item, empty);
ObservableList<Node> children = container.getChildren();
int newLength = (empty || item == null) ? 0 : item.length;
if (newLength < children.size()) {
// remove ImageViews that are not required
for (int i = newLength; i < children.size(); i++) {
ImageView iv = (ImageView) children.get(i);
imageViews.add(iv); // allow reuse of imageviews
iv.setImage(null); // don't prevent garbagecollection of image
}
children.remove(newLength, children.size());
}
// add missing imageviews
for (int i = children.size(); i < newLength; i++) {
children.add(getOrCreateImageView());
}
// fill imageviews
for (int i = 0; i < newLength; i++) {
((ImageView) children.get(i)).setImage(item[i]);
}
setGraphic(newLength == 0 ? null : container);
}
public static <E> Callback<TableColumn<E, Image[]>, TableCell<E, Image[]>> forTableColumn() {
List<ImageView> views = new ArrayList<>();
return c -> new ImagesTableCell<>(views);
}
}
pic.setCellFactory(ImagesTableCell.forTableColumn());