I’ve been thinking of publishing this simple, yet often useful wrapper for a while. And damn, Sami made
an almost identical wrapper today as well… :huh:
Anyhow, here’s my version. You can use it easily, for example:
// An array
Integer[] numbers = {4, 8, 15, 16, 23, 42};
// The wrapper
ArrayContainer<Integer> numberCtn = new ArrayContainer<Integer> (numbers);
// The column property ID is used by Table as the default for
// column header. It is by default "0".
numberCtn.setColumnPropertyId("Number");
// Use the wrapper as the data source for the table
Table numbersTbl = new Table("Integers", numberCtn);
mainWindow.addComponent(numbersTbl);
I added a
LazyContainer class suggested by Sami. It allows fully dynamic generation of container data from a callback interface. It’s also a much easier alternative to
Table.ColumnGenerator for calculating the values of one column from another.
// 100,000 rows and 2 columns
LazyContainer lazydata = new LazyContainer(100000, 2, new LazyContainer.Provider() {
public Object getData(int row, int column) {
if (column == 0)
return row;
else if (column == 1)
return ((double)row)*((double)row);
else
return null;
}
});
Table table = new Table("Data from a Lazy Container", lazydata);
table.setColumnHeaders(new String[] {"Index", "Power of Index"});
mainWindow.addComponent(table);
Notice that a Table is unable to hold more than about 500,000 items. The exact limit depends on the font size, because the table content is rendered as a very tall HTML layer.