Kevvvvyp
Kevvvvyp

Reputation: 1774

Dynamically add table columns?

I'm looking for a way to dynamically add columns to a vaadin table.

I tried this:

private Button createAddColumnButton() {
    Button addProductButton = new Button("Add column");

    addProductButton.addClickListener(new ClickListener() {

        public void buttonClick(ClickEvent event) {
            count = count++;
            table.addGeneratedColumn("Column "+count, new ColumnGenerator() {

                @Override
                public Object generateCell(final Table source, Object itemId, Object columnId) {
                    String x = "some stuff";
                    return x;
                }
            });
        }
    });
    return addProductButton;
}

This button allowed me dynamically add a column, however only one column before I recieved an error saying I cannot have two columns with the same id. How can I change the ID so it is unique & add lots of columns?

Upvotes: 0

Views: 887

Answers (1)

kukis
kukis

Reputation: 4644

TL;DR

Simple change your code to:

count = count + 1;

Explenation

That's beacause assigment

count = count++;

does not work in the way you expect. Take a look at the following code:

public class HelloStackOverflow {
    public static void main(String[] args) {
        int count = 0;
        count = count++;
        System.out.println(count);
    }
}

This prints on standard output 0. You will even get warning (The assignment to variable count has no effect) if you change your code to:

count = ++count;

You can find even better explanation here.

Upvotes: 3

Related Questions