'How can I get the row, column clicked in gwt table?

I have a flextable that gets populated with data from a database.

I want to get the ROW number of the clicked row.

So far I figured out only how to get the value of a particular cell in a particular row. You have to know the position and hard code it which isn't practical.

 String test =flexTable.getFlexCellFormatter().getElement(2, 2).getInnerHTML();
 System.out.println(test);

How can I create a ClickHandler to get the selected row?

gwt


Solution 1:[1]

//flexTable is a FlexTable object. Add a ClickHandler to it.
flexTable.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {            
            //gets the index of the cell you clicked on
            int cellIndex = flexTable.getCellForEvent(event).getCellIndex();
            //gets the index of the row you clicked on
            int rowIndex = flexTable.getCellForEvent(event).getRowIndex();
            //print statements below will verify 
            System.out.println("cellIndex "+cellIndex);
            System.out.println("rowIndex "+rowIndex);
            //gets the value of the selected cell
            String test =flexTable.getFlexCellFormatter().getElement(rowIndex,cellIndex ).getInnerHTML();
            System.out.println(test);
                
        }
    });

Solution 2:[2]

The FlexTable supports row spans and column spans, allowing you to layout data in a variety of ways. I think you should use CellTable or a DataGrid or for that matter even a CellList if only single column data is being displayed. To these widgets you can add a selectionhandler to which you can achieve your goal. Refer this it has some pre coded examples.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimantha
Solution 2 Onkar