I want to create listView item with multiple labels and images. I created a cell class:
public class Cell extends ListCell<String> {
private HBox hBox;
private Label description;
private Pane pane;
private ImageView imageView;
private Image deleteImage;
private JFXButton delete;
public Cell() {
super();
this.hBox = new HBox();
this.description = new Label();
this.pane = new Pane();
this.delete = new JFXButton("delete");
this.hBox.getChildren().addAll(description, pane, delete);
HBox.setHgrow(pane, Priority.ALWAYS);
delete.setOnAction(event -> getListView().getItems().remove(getItem()));
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(null);
setGraphic(null);
if (item != null && !empty) {
description.setText(item);
setGraphic(hBox);
}
}
This is part of my controller code:
void addToShopCart(ActionEvent event) {
Image partImage = selectedPart.getPicture();
shopListView.getItems().addAll(selectedPart.getDescription());
shopListView.setCellFactory(param -> new Cell(partImage));
}
selectedpart my custom class which has an image. But now I have no idea how to send texts to these labels, because updateItem() method gets only one String. And about image, I want to set different image to each item seperately. I tried to send to constructor, but then all images are the same. I am using MVC and I can also work with code.