I want to get the selected row in a GWT CellTable when the user pressed down the keys up or down. In my example I have a table with three columns (ID, name, date) and I have implemented the code to handle the click event. When clicking a row I get the selected object. Now I want to do the same when moving through the rows of the table using the arrows keys up and down.
This is my code:
// Create id column.
TextColumn<MyObject> idColumn = new TextColumn<MyObject>()
{
@Override
public String getValue(MyObject obj)
{
return String.valueOf(obj.getId());
}
};
// Create name column.
TextColumn<MyObject> nameColumn = new TextColumn<MyObject>()
{
@Override
public String getValue(MyObject obj)
{
return obj.getName();
}
};
// Create date column.
TextColumn<MyObject> dateColumn = new TextColumn<MyObject>()
{
@Override
public String getValue(MyObject obj)
{
return String.valueOf(obj.getDate());
}
};
// Create the CellTable element and add columns
CellTable<MyObject> table = new CellTable<MyObject>();
table.addColumn(idColumn, "ID");
table.addColumn(nameColumn, "Name");
table.addColumn(dateColumn, "Date");
// Create the selectioModel and the SelectionChangeEvent Handler
NoSelectionModel<MyObject> selectionModelMyObj = new NoSelectionModel<MyObject>();
Handler tableHandler = new SelectionChangeEvent.Handler()
{
@Override
public void onSelectionChange(SelectionChangeEvent event)
{
MyObject clickedObj = selectionModelMyObj.getLastSelectedObject();
Window.alert("Object selected: " + clickedObj);
}
};
// Add the handler to the selection model
selectionModelMyObj.addSelectionChangeHandler( tableHandler );
// Add the selection model to the table
table.setSelectionModel(selectionModelMyObj);
Thanks for all.
By default, keyboard navigation within a CellTable
only updates the keyboard selected row. Only by hitting the enter key the row will become selected.
If you want the selection to follow the keyboard selection, the you can set the keyboard selection policy to be bound to selection. I'm not sure how it'll work with a NoSelectionModel
though, would probably work better with a SingleSelectionModel
.