// Create a CellTable with a key provider.
final CellTable<Contact> table = new CellTable<Contact>(KEY_PROVIDER);
table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
// Add a text input column to edit the name.
final TextInputCell nameCell = new TextInputCell();
Column<Contact, String> nameColumn = new Column<Contact, String>(nameCell) {
@Override
public String getValue(Contact object) {
return object.name;
}
};
table.addColumn(nameColumn, "Name");
// Add a field updater to be notified when the user enters a new name.
nameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {
@Override
public void update(int index, Contact object, String value) {
// Validate the data.
if (value.length() < 3) {
Window.alert("Names must be at least three characters long.");
/*
* Clear the view data. The view data contains the pending change and
* allows the table to render with the pending value until the data is
* committed. If the data is committed into the object, the view data
* is automatically cleared out. If the data is not committed because
* it is invalid, you must delete.
*/
nameCell.clearViewData(KEY_PROVIDER.getKey(object));
// Redraw the table.
table.redraw();
return;
}