IColumnPropertyAccessor<Person> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<Person>(
propertyNames);
IDataProvider bodyDataProvider = new ListDataProvider<Person>(
PersonService.getPersons(10), columnPropertyAccessor);
final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
// use different style bits to avoid rendering of inactive scrollbars
// for small table
// Note: The enabling/disabling and showing of the scrollbars is handled
// by the ViewportLayer.
// Without the ViewportLayer the scrollbars will always be visible with
// the default
// style bits of NatTable.
final NatTable natTable = new NatTable(parent, SWT.NO_BACKGROUND
| SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED, bodyDataLayer);
GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
Button b1 = new Button(parent, SWT.PUSH);
b1.setText("Toggle column width");
b1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
showDefaultColumnWidth = !showDefaultColumnWidth;
if (showDefaultColumnWidth) {
// reset to default
bodyDataLayer.setColumnWidthByPosition(0,
DataLayer.DEFAULT_COLUMN_WIDTH, false);
bodyDataLayer.setColumnWidthByPosition(1,
DataLayer.DEFAULT_COLUMN_WIDTH, false);
bodyDataLayer.setColumnWidthByPosition(2,
DataLayer.DEFAULT_COLUMN_WIDTH, false);
bodyDataLayer.setColumnWidthByPosition(3,
DataLayer.DEFAULT_COLUMN_WIDTH, false);
// this one will trigger the refresh
bodyDataLayer.setColumnWidthByPosition(4,
DataLayer.DEFAULT_COLUMN_WIDTH, true);
} else {
bodyDataLayer.setColumnWidthByPosition(0, 70, false);
bodyDataLayer.setColumnWidthByPosition(1, 70, false);
bodyDataLayer.setColumnWidthByPosition(2, 50, false);
bodyDataLayer.setColumnWidthByPosition(3, 30, false);
// this one will trigger the refresh
bodyDataLayer.setColumnWidthByPosition(4, 200, true);
}
}
});
Button b2 = new Button(parent, SWT.PUSH);
b2.setText("Toggle row height");
b2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
showDefaultRowHeight = !showDefaultRowHeight;
if (showDefaultRowHeight) {
// reset to default
bodyDataLayer
.setDefaultRowHeight(DataLayer.DEFAULT_ROW_HEIGHT);
} else {
bodyDataLayer.setDefaultRowHeight(50);
}
// repaint the table, as setting the default height is not
// triggering a refresh automatically
// this is because setting the default usually should be done