Button in a table

We have several tables in an application that have buttons in rows. In Vaadin 6 we did that by using TableFieldFactory, but since Button is no longer a Field we cannot use a Button to return in getField() method.

We are investigating to solve that. If anyone had the same problem and has found a solution we will like to know.

Here is a sample of what we do:


TableFieldFactory chkFF = new TableFieldFactory() {
  @Override
  public Field createField(Container container, Object itemId, Object propertyId, Component uiContext) {
    Field f;
    if ("buttonField".equals(propertyId)) {
      Button bt = new Button(); 
      bt.addClickListener(new Button.ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
          // Do something
        }
      });
      f = bt;
    } else {
      f = DefaultFieldFactory.get().createField(container, itemId, propertyId, uiContext);
    }
    return f;
  }
};

Thank you in advance.

Jose,

you could use Table.addGeneratedColumn(). This method takes a ColumnGenerator as an argument, which is responsible to create the cells for a column. If it returns components in its generateCell() method, these will be rendered inside the table.

Stefan

Hi, I did it so and it worked. Thank you very much.

Example:


table.addGeneratedColumn("buttonField", new Table.ColumnGenerator() {
  @Override
  public Object generateCell(Table source, Object itemId, Object columnId) {
    Button bt = new Button("...");
    bt.setData(itemId);
    bt.addClickListener(mylistener);
    return bt;
  }
});