3

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.

Victor_JF
  • 31
  • 1
  • 2

3 Answers3

4

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.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • Thanks for the information. I didn`t know how the CellTable worked. I'll use the SingleSelectionModel instead of the NoSelectionModel, it works better for my program. Thanks. – Victor_JF Jan 23 '13 at 08:29
1

You need to understand the Keyboard Selection policy w.r.t to GWT widgets and celltable.

The right way is to allow only row navigation with Keys "UP" and "Down" and cell navigation with Keys "Left" and "Right" . The selection of rows should be only on click of "Space" or "Enter" key.

All of this is already provided with GWT cell table. You can check the source code and demo http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellTable

appbootup
  • 9,537
  • 3
  • 33
  • 65
1

You can get the row of a CellTable with the getKeyboardSelectedRow() method. This will return the int index of the row that is selected via either the keyboard or mouse.

GWT Javadoc for CellTable: http://www.gwtproject.org/javadoc/latest/

public int getKeyboardSelectedRow()

Get the index of the row that is currently selected via the keyboard, relative to the page start index.

This is not same as the selected row in the SelectionModel. The keyboard selected row refers to the row that the user navigated to via the keyboard or mouse.

Returns: the currently selected row, or -1 if none selected