Delete row in Beancontainer table

Hey guys,
i’m new to Vaadin.
I created a table that shows the content of a Beancontainer.
Adding a component to the container is simple, but i would like to delete a component by
selecting it in the table and clicking delete. Do I need some sort of listener for the table or
how am i able to get the bean of the selected row.

Here is my code so

package com.example.test;

import java.awt.List;

public class Testnew extends VerticalLayout implements ClickListener{


    private static final long serialVersionUID = 1L;
   
    BeanContainer<String, Person> beans=new BeanContainer<String, Person>(Person.class);
    Table table=new Table("Meine Tabelle", beans);
    TextField name=new TextField("Name");
    TextField vorname=new TextField("Vorname");
    TextField email=new TextField("Email-Adresse");
    Button button=new Button("Add");
    Button button2=new Button("Delete");
    
       public Testnew() {
        beans.setBeanIdProperty("name");
        table.setSelectable(true);
        table.setImmediate(true);
        table.setPageLength(0);
        table.setWidth("80%");
        VerticalLayout newlayout = new VerticalLayout();
        newlayout.setWidth("50%");
        newlayout.setSpacing(true);
        this.setSizeFull();
        
        button.addClickListener(this);
        button2.addClickListener(this);
        newlayout.addComponent(name);
        newlayout.addComponent(vorname);
        newlayout.addComponent(email);
        newlayout.addComponent(button);
        newlayout.addComponent(button2);
        newlayout.addComponent(table);
        newlayout.setComponentAlignment(name, Alignment.TOP_CENTER);
        newlayout.setComponentAlignment(vorname, Alignment.TOP_CENTER);
        newlayout.setComponentAlignment(email, Alignment.TOP_CENTER);
        newlayout.setComponentAlignment(button, Alignment.TOP_CENTER);
        newlayout.setComponentAlignment(button2, Alignment.TOP_CENTER);
        
        newlayout.setSpacing(true);
        this.addComponent(newlayout);
        this.setComponentAlignment(newlayout, Alignment.MIDDLE_CENTER);
    }
 

    public void buttonClick(ClickEvent event) {
        if (event.getButton() == button){
        beans.addBean(new Person(name.getValue().toString(),vorname.getValue().toString(),email.getValue().toString()));
        name.setValue("");
        vorname.setValue("");
        email.setValue("");
        Notification.show("Eintrag hinzugefügt");
        } else if (event.getButton() == button2){
            //Dont know how to remove the Item
            //beans.removeItem();
            Notification.show("Eintrag eventuell gelöscht");
    }
            
        }
}
    

Thanks for any kind of help :slight_smile:

You can add a ValueChangeListener to your table (https://vaadin.com/book/vaadin7/-/page/components.table.html, Topic 5.16.1).

    public Testnew() {
        ...
        table.setImmediate(true);

        table.addValueChangeListener(new Property.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                selected = table.getValue();
            }
        });
    }

    public void buttonClick(ClickEvent event) {
        if (event.getButton() == button) {
          ...
        } else if (event.getButton() == button2) {
            if (selected != null) {
                beans.removeItem(selected);
            }
            ...
        }
    }