Add table column with BeanItemContainer

java, vaadin, vaadin7

Solution

You could create a `ColumnGenerator` which creates the button for you. Have a look here.

Example:

Let's say we have a MyBean class:

public class MyBean {

    private String sDesignation;
    private int iValue;

    public MyBean() {
    }

    public MyBean(String sDesignation, int iValue) {
        this.sDesignation = sDesignation;
        this.iValue = iValue;
    }

    public String getDesignation() {
        return sDesignation;
    }

    public int getValue() {
        return iValue;
    }

}

We then can create a table with a generated column giving a button to delete the current item.

Table table = new Table();

BeanItemContainer<MyBean> itemContainer = new BeanItemContainer<MyBean>(MyBean.class);
table.setContainerDataSource(itemContainer);

table.addItem(new MyBean("A", 1));
table.addItem(new MyBean("B", 2));

table.addGeneratedColumn("Action", new ColumnGenerator() { // or instead of "Action" you can add ""
    @Override
    public Object generateCell(final Table source, final Object itemId, Object columnId) {
        Button btn = new Button("Delete");
        btn.addClickListener(new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                source.removeItem(itemId);
            }
        });
        return btn;
    }
});

table.setVisibleColumns(new Object[]{"designation", "value", "Action"}); // if you added "" instead of "Action" replace it by ""

Problem

I should add a column to a table that has a BeanItemContainer datasource. This is my situation: I hava an entity bean ``` @Entity public class MyBean implements { @Id private Long id; //other properties ``` } Then in my vaadin panel i have this method ``` private Table makeTable(){ Table table = new Table(); tableContainer = new BeanItemContainer<MyBean>(MyBean.class); table.setContainerDataSource(tableContainer); table.setHeight("100px"); table.setSelectable(true); return table; } ``` Now, I want to add a column that should give me the ability to delete an item in this container. How can i do?

Original source