Examples
In this section you will find some examples on how to use the NatTable.
Creating a basic grid
- Assemble the Layer stacks depending on the features you wish to enable. Every region has a layer stack backing it.
- Add/modify configuration object to customize behaviour.
- Reorder columns
- Hide columns
- Scrolling
- Selection
Plugging data
The primary interface for providing data to NatTable is IDataProvider. The most common way of providing data to the table is to use a List data structure. This list contains an object for each row in the table. Each property of the row object is represented in a column.
In this example we are using a POJO named person to represent the data in a row.
If you are using a List as your data structure, you can use the ListDataProvider out of the box. In this case our data provider will look like so
The ReflectiveColumnPropertyAccessor uses standard java getter methods to read data from the row object. If you wish to fetch data from your row object in specific ways, you can plugin a custom IColumnPropertyAccessor here.
Setting up the body region
In this stack we are plugging in the column reorder, column hide show, selection layer and scrolling functionality. The viewport is a window into the complete data set. In other words this is the rows/columns visible in the table. As you scroll the viewport moves over the underlying data set. The data provider should be wrapped up by the DataLayer. The DataLayer is always the lowermost layer in the stack. Its is responsible for providing data to the grid.
Setting up the column header region
Since the column header has a dependence on the body layer and hence inherits features from it. All it needs to do in most cases is to have a data provider. This data provider will supply data for the column labels.
Setting up the row header layer
The row header is similar the column header. Note that the data layer also tracks the sizes of the rows/columns. Hence, you can set the default sizes in the constructor for the data layer.
Setting up the corner layer
The corner layer derives all its feature set from the column and row header layers. Hence, it can be set up very simply by passing in the dependents.
Drum roll ...
Now we have setup layer stacks for all regions in the grid. These stacks need to be unified to work as a coherent whole. We do this by placing a grid layer on the top. This layer is set as the underlying layer for NatTable and we are all ready to go.