0

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?

Kevvvvyp
  • 1,704
  • 2
  • 18
  • 38
  • I have a feeling I need to create a class that implements Column generator and in it set its ID to a random int? maybe...? – Kevvvvyp Dec 09 '14 at 10:37
  • 1
    The "column ID" is the first parameter to `addGeneratedColumn()`, so it looks like it is unique in your example. Where is count declared? Is it changed elsewhere? Can you show a full stack trace? – geert3 Dec 09 '14 at 10:45

1 Answers1

3

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.

Community
  • 1
  • 1
kukis
  • 4,489
  • 6
  • 27
  • 50