Frage

I want to add double click handler to FlexTable in GWT.

I used below code :

 flexTable.addDoubleClickHandler(new DoubleClickHandler() {

                @Override
                public void onDoubleClick(DoubleClickEvent event) {

                }
    });

But how can i get row index.

Please suggest me.

War es hilfreich?

Lösung

Sadly there is no built-in method to retrieve a cell from a double-click event in FlexTable. It can be implemented in a few lines though. This is how I did it.

Create a subclass of FlexTable with the following code:

public class DoubleClickTable extends FlexTable {
    class MyCell extends Cell {
        protected MyCell(int rowIndex, int cellIndex) {
            super(rowIndex, cellIndex);
        }
    }

    public Cell getCellForEvent(MouseEvent<? extends EventHandler> event) {
        Element td = getEventTargetCell(Event.as(event.getNativeEvent()));
        if (td == null) {
          return null;
        }

        int row = TableRowElement.as(td.getParentElement()).getSectionRowIndex();
        int column = TableCellElement.as(td).getCellIndex();
        return new MyCell(row, column);
    }
}

Then in the DoubleClickHandler call getCellForEvent() to retrieve the clicked cell:

flexTable.addDoubleClickHandler(new DoubleClickHandler() {
    @Override
    public void onDoubleClick(DoubleClickEvent event) {
        Cell cell = flexTable.getCellForEvent(event);
        GWT.log("Row index: " + cell.getRowIndex());
    }
});

Implementation details: method getCellForEvent() is a copy of the method with the same name in class HTMLTable (the parent class of FlexTable), except that it has a different signature for the parameters. The MyCell class is a workaround to be able to call the Cell constructor, which is protected.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top