Unlike the list that uses the render approach to create exceptionally large lists without much of an overhead, the table is a more "stateful" component and uses a more conventional approach of nesting components.
A table is an editable grid component with variable sizes for its entries. Entries can be editable or not. Just like the list, the table has a model (TableModel) and a default model implementation (DefaultTableModel).
To create a table instance, a developer needs to first instantiate a model with the data and then create a table as follows:
TableModel model = new DefaultTableModel(new String[] { "Col 1", "Col 2", "Col 3" }, new Object[][] { {"Row 1", "Row A", "Row X"}, {"Row 2", "Row B", "Row Y"}, {"Row 3", "Row C", "Row Z"}, {"Row 4", "Row D", "Row K"}, }); Table table = new Table(model);
The table created by the example code is shown in Figure 1.
Figure: Table example
A cell can be made editable by overriding the isCellEditable method of the model as follows:
public boolean isCellEditable(int row, int col) { return col != 0; }
The table component contains a few more elaborate features, such as the ability to span columns and rows and determine their width or height as percentage of available space. A table can be made to scroll on the X axis as well by setting it to setScrollableX(true), in which case it can "grow" beyond the screen size.
To control the "rendering", the way in which a table creates the cells within it, one needs to derive the table itself and override the method createCell like this:
Table table = new Table(model) { protected Component createCell(Object value, int row, int column, boolean editable) { // custom code for creating a table cell ... } };
Notice that components created using createCell are "live" for the duration of the table's existence and are able to receive events and animate. They also occupy resources for the duration of the table's existence.