Vaadin 8, Grid, listener on column

Could you supply an example how to add a listener on a column?
myGrid.getColum(“myTrashIconColumn”).addListener(event ???, target???, method???)

My target is : if you click on Trash icon column, the item/row will be deleted.
TIA
Danilo

See
https://vaadin.com/docs/-/part/framework/components/components-grid.html

Vaadin 8.0.x

List<Person> people = new ArrayList<>();
people.add(new Person("Nicolaus Copernicus", 1473));
people.add(new Person("Galileo Galilei", 1564));
people.add(new Person("Johannes Kepler", 1571));

// Create a grid
Grid<Person> grid = new Grid<>(people);

// Render a button that deletes the data row (item)
grid.addColumn(person -> "Delete", new ButtonRenderer(clickEvent -> {
      people.remove(clickEvent.getItem());
      grid.setItems(people);
}));

Vaadin 8.1.0 (beta)

Use the example above or use addComponentColumn for more flexibility

grid.addComponentColumn(person ->
{
    Button button = new Button("Delete");
    button.addClickListener(click -> {
       people.remove(clickEvent.getItem()); 
       grid.setItems(people);
    });
    return button;
});
// make sure the buttons fit in the cells of the Grid
grid.setRowHeight(40);

It works, thanks a lot.

Hi, I am in the same scenario of Danilo, I just want to set Enable the button only when user select the element in the grid. How is this possibile?

If you use case is delete, you probably would like some sort of “Delete” → “Confirm” → pattern, and I made custom DeleteButtonRenderer for that purpose in https://vaadin.com/directory/component/grid-renderers-collection-for-vaadin7

Tatu Lund:
If you use case is delete, you probably would like some sort of “Delete” → “Confirm” → pattern, and I made custom DeleteButtonRenderer for that purpose in https://vaadin.com/directory/component/grid-renderers-collection-for-vaadin7

Thank you for your reply, I already implemented a ConfirmationWindow for deleting the object after pressing the Cancel Button, what I need to do is to enable the cancel button only when the item from grid is selected.