您可以創建一個ColumnGenerator
,它爲您創建按鈕。 看一看here。
例子:
比方說,我們有一個爲myBean類:然後
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;
}
}
我們可以創建一個表,生成列賦予一個按鈕,刪除當前項目。
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 ""
我試圖用這個解決方案,但我得到java.lang.NoClassDefFoundError上table.addGeneratedColumn(「」,新ColumnGenerator()。爲什麼呢? – Skizzo
@Marco更新了我從一個工作示例答案。 – nexus
由於現在一切正常 – Skizzo