Adding clickHandler to row in CellTable in GWT?

六月ゝ 毕业季﹏ 提交于 2019-11-27 20:13:50
Hilbrand Bouwkamp

A CellTable has built in support for handling click events. You can add a CellPreviewHandler that will be called among others when a row is clicked. It will receive a number of items in the event like the native event, cell, and data row value. Because it fires not only for click events you need to check if the click event was fired. Simply test the event passed: boolean isClick = "click".equals(event.getNativeEvent().getType()).

Another option is to extend the protected method doSelection, but it's deprecated and in you need to als make sure you have the right KeyboardSelectionPolicy set to make sure it's called when a click is done. See for the latter in the JavaDoc of the interface KeyboardSelectionPolicy.

Another way to have a cell selected can be made using a NoSelectionModel and add it to the table:

//EDIT: this is a field, not a local variable
TheCellObject clickedObject; //the object selected by selectionModel

final NoSelectionModel<TheCellObject> selectionModel = new NoSelectionModel<TheCellObject>();

    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

                @Override
                public void onSelectionChange(SelectionChangeEvent event) {
                    clickedObject = selectionModel.getLastSelectedObject();
                }
            });
cellTable.setSelectionModel(selectionModel); //add selection model to your celltable
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!