Dynamically adding columns to a table

Vaadin noob here.

I’m trying to add columns to a Table on-the-fly by adding container properties to an indexed container, but the new columns do not appear in the table. Help!!!

public class MyFirstVaadinUi extends UI {

/*
 * After UI class is created, init() is executed. You should build and wire
 * up your user interface here.
 */
protected void init(VaadinRequest request) {

    final VerticalLayout content = new VerticalLayout();
    setContent(content);

    Button addColBtn = new Button("Add Column");
    content.addComponent(addColBtn);

    final IndexedContainer container = new IndexedContainer();
    container.addItem("Jon");

    container.addContainerProperty("Name", String.class, "");

    container.getContainerProperty("Jon", "Name").setValue("blah");

    addColBtn.addClickListener(new ClickListener() {
        @Override
        public void buttonClick(ClickEvent clickEvent) {
            String newProperty = randomString();

            container.addContainerProperty(newProperty, String.class, "");

            for (Object existingRow : container.getItemIds()) {
                container.getContainerProperty(existingRow, newProperty).setValue(randomString());
            }
        }
    });

    final Table tbl = new Table();
    tbl.setImmediate(true);
    tbl.setContainerDataSource(container);
    tbl.setSizeFull();
    content.addComponent(tbl);
}

private String randomString() {
    return "" + System.currentTimeMillis();
}

}

Your table was initialized with the original set of propertyIDs of your container, you should add a Container.PropertySetChangeListener to your container , check if any of the container’s propertyIDs are not displayed in the table , if so call table.setVisibleColumns(…) with all your desired propertyIDs …