0
votes

In GWT 2.6 CellTable, I'm writing a single click event to perform some operation. I can not get the correct Row Index while clicking on the CellTable Row; only a double click event returns the row correctly.

final SingleSelectionModel<PatientDTO> selectionModel = 
    new SingleSelectionModel<PatientDTO>();

patientsTable.setSelectionModel(selectionModel);  

patientsTable.addDomHandler(new ClickHandler()  
{  
    @Override  
    public void onClick(ClickEvent event)  
    {
        PatientDTO selected = selectionModel.getSelectedObject();
        if (selected != null) 
        {
            RootLayoutPanel.get().clear();
            RootLayoutPanel.get().add(new PatientPanel(selected));
        }
    }
}, ClickEvent.getType());
1

1 Answers

0
votes

Either use SingleSelectionModel or MultiSelectionModel and add SelectionChangeHandler on it that will be fired when selection is changed in the CellTable

Sample code:

final SingleSelectionModel<Contact> selectionModel = new SingleSelectionModel<Contact>();
//final MultiSelectionModel<Contact> selectionModel = new MultiSelectionModel<Contact>();
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
        Set<Contact> selected = selectionModel.getSelectedSet();
        if (selected != null) {
            for (Contact contact : selected) {
                System.out.println("You selected: " + contact.name);
            }
        }
    }
});

OR alternatively try with CellPreviewHandler

table.addCellPreviewHandler(new Handler<Contact>() {

    @Override
    public void onCellPreview(CellPreviewEvent<Contact> event) {
        int row = event.getIndex();
        int column = event.getColumn();

        if ("focus".equals(event.getNativeEvent().getType())) {
           //..
        }
        if ("blur".equals(event.getNativeEvent().getType())) {
            //...
        }
        if ("mousedown".equals(event.getNativeEvent().getType())) {
            //..
        }
        if ("mouseover".equals(event.getNativeEvent().getType())) {
            //..
        }
    }

});