Understanding generated columns
Sometimes you need to calculate the content of one column based on the content of other columns. Generated columns are just great for accomplishing that. Suppose you have a table with two Integer
columns:
Table table = new Table(); table.addContainerProperty("A", Integer.class, 0); table.addContainerProperty("B", Integer.class, 0);
You want a third column with the sum of the previous columns. Here is the code to do that:
table.addGeneratedColumn("A + B", new ColumnGenerator() { @Override public Object generateCell(Table source, Object itemId, Object columnId) { Integer a = (Integer) source.getItem(itemId) .getItemProperty("A").getValue(); Integer b = (Integer) source.getItem(itemId) .getItemProperty("B").getValue(); return a + b; } });
A column generator must implement the generateCell
method that returns the value or the UI component to render on the cell...