你怎么称呼“我的朋友”;getCellForEvent();对于所有GWT网格鼠标事件?

你怎么称呼“我的朋友”;getCellForEvent();对于所有GWT网格鼠标事件?,gwt,Gwt,对于GWT,当您有一个网格时,我想获取哪些行和列的事件 因此,我扩展了GWT网格以添加鼠标事件: @Override public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) { return this.addDomHandler(handler, MouseOverEvent.getType()); } @Override public HandlerRegistration addMouseOu

对于GWT,当您有一个网格时,我想获取哪些行和列的事件

因此,我扩展了GWT网格以添加鼠标事件:

@Override
public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
    return this.addDomHandler(handler, MouseOverEvent.getType());
}

@Override
public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
    return this.addDomHandler(handler, MouseOutEvent.getType());
}

@Override
public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
    return this.addDomHandler(handler, MouseMoveEvent.getType());
}
然后注册事件处理程序以单击鼠标:

// mouse event handler
g.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        Cell cell = ((HTMLTable)event.getSource()).getCellForEvent(event);
        System.out.println("GridClickHandler: ("+cell.getRowIndex()+","+cell.getCellIndex()+")");
    }
});
现在我得到了特定单元格的事件。。。但仅适用于单击鼠标时。如何为所有鼠标事件调用“getCellForEvent()”?

public class MyTable extends Grid {
    public MyTable(){
       sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
    }

    @Override
    public void onBrowserEvent(Event event) {
      super.onBrowserEvent(event);
      Element td = getEventTargetCell(event);
      if (td == null) {
         return;
      }
      Element tr = DOM.getParent(td);
       switch (DOM.eventGetType(event)) {
         case Event.ONMOUSEOVER: {
            tr.addClassName("my-tbl-item-sel");
            break;
         }
         case Event.ONMOUSEOUT: {
            tr.removeClassName("my-tbl-item-sel");
            break;
         }
    } 
}
试试这个

我得到了适用于所有鼠标事件类型的getCellForEvent()函数(而不仅仅是单击事件)

获得所需行为的方法是扩展gwt的网格。然后深入研究getCellForEvent()的位置,并将这段代码复制到新的扩展网格中。将getCellForEvent()从“ClickEvent”更改为“MouseeEvent”

onModuleLoad():

新的扩展网格类:

包com.agilent.gridDisplay.client

import com.google.gwt.dom.client.*
import com.google.gwt.event.dom.client.*
import com.google.gwt.event.shared.*
import com.google.gwt.user.client.*
import com.google.gwt.user.client.ui.*;

public class GridWithMouse 
       extends Grid
       implements HasMouseOutHandlers, HasMouseOverHandlers, HasMouseMoveHandlers
{

    /**
     * Return value for {@link HTMLTable#getCellForEvent}.
     */
    public class Cell extends com.google.gwt.user.client.ui.HTMLTable.Cell{
        public Cell(int rowIndex, int cellIndex) {
            super(rowIndex, cellIndex);
        }
    }

    public GridWithMouse(int rows, int cols) {
        super(rows,cols);
        for(int i = 0; i < 2; i++) {
            for(int j = 0; j < 2; j++) {
                this.setWidget(i,j,new Label("hello"+i));
            }
        }
    }

    /**
     * Given a click event, return the Cell that was clicked, or null if the event
     * did not hit this table.  The cell can also be null if the click event does
     * not occur on a specific cell.
     * 
     * @param event A click event of indeterminate origin
     * @return The appropriate cell, or null
     */
    @SuppressWarnings("rawtypes")
    public Cell getCellForEvent(MouseEvent 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 Cell(row, column);
    }

    @Override
    public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
        return this.addDomHandler(handler, MouseOverEvent.getType());
    }

    @Override
    public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
        return this.addDomHandler(handler, MouseOutEvent.getType());
    }

    @Override
    public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
        return this.addDomHandler(handler, MouseMoveEvent.getType());
    }

}
import com.google.gwt.dom.client*
导入com.google.gwt.event.dom.client*
导入com.google.gwt.event.shared*
导入com.google.gwt.user.client*
导入com.google.gwt.user.client.ui.*;
公共类GridWithMouse
扩展网格
实现HasMouseOutHandler、HasMouseOverHandler、HasMouseMoveHandler
{
/**
*{@link HTMLTable#getCellForEvent}的返回值。
*/
公共类单元格扩展com.google.gwt.user.client.ui.HTMLTable.Cell{
公共单元格(int行索引,int单元格索引){
超级(行索引、单元格索引);
}
}
带鼠标的公共网格(int行、int列){
超级(行、列);
对于(int i=0;i<2;i++){
对于(int j=0;j<2;j++){
这个.setWidget(i,j,新标签(“hello”+i));
}
}
}
/**
*给定一个click事件,返回被单击的单元格,如果该事件发生,则返回null
*未命中此表。如果单击事件未命中,则单元格也可以为空
*不发生在特定单元格上。
* 
*@param事件来源不确定的点击事件
*@返回相应的单元格,或null
*/
@抑制警告(“原始类型”)
公共单元格getCellForEvent(MouseeEvent事件){
元素td=getEventTargetCell(Event.as(Event.getNativeEvent());
if(td==null){
返回null;
}
int row=TableRowElement.as(td.getParentElement()).getSectionRowIndex();
int column=TableCellElement.as(td).getCellIndex();
返回新单元格(行、列);
}
@凌驾
公共句柄注册addMouseOverHandler(MouseOverHandler处理程序){
返回此.addDomHandler(handler,MouseOverEvent.getType());
}
@凌驾
公共句柄注册addMouseOutHandler(MouseOutHandler句柄){
返回此.addDomHandler(handler,MouseOutEvent.getType());
}
@凌驾
公共句柄注册addMouseMoveHandler(MouseMoveHandler){
返回此.addDomHandler(handler,MouseMoveEvent.getType());
}
}

现在,不是为所有鼠标事件获取像素位置,而是为所有鼠标事件获取网格位置。。。这才是网格从一开始就需要的

@JamshidAsatillayev,我已经让getCellForEvent为ClickEvents工作了。我需要用于MouseOverEvent、MouseOutEvent等。我已经有了用于ClickEvents的getCellForEvent。我需要它来做MouseOverEvent、MouseOutEvent等
import com.google.gwt.dom.client.*
import com.google.gwt.event.dom.client.*
import com.google.gwt.event.shared.*
import com.google.gwt.user.client.*
import com.google.gwt.user.client.ui.*;

public class GridWithMouse 
       extends Grid
       implements HasMouseOutHandlers, HasMouseOverHandlers, HasMouseMoveHandlers
{

    /**
     * Return value for {@link HTMLTable#getCellForEvent}.
     */
    public class Cell extends com.google.gwt.user.client.ui.HTMLTable.Cell{
        public Cell(int rowIndex, int cellIndex) {
            super(rowIndex, cellIndex);
        }
    }

    public GridWithMouse(int rows, int cols) {
        super(rows,cols);
        for(int i = 0; i < 2; i++) {
            for(int j = 0; j < 2; j++) {
                this.setWidget(i,j,new Label("hello"+i));
            }
        }
    }

    /**
     * Given a click event, return the Cell that was clicked, or null if the event
     * did not hit this table.  The cell can also be null if the click event does
     * not occur on a specific cell.
     * 
     * @param event A click event of indeterminate origin
     * @return The appropriate cell, or null
     */
    @SuppressWarnings("rawtypes")
    public Cell getCellForEvent(MouseEvent 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 Cell(row, column);
    }

    @Override
    public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
        return this.addDomHandler(handler, MouseOverEvent.getType());
    }

    @Override
    public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
        return this.addDomHandler(handler, MouseOutEvent.getType());
    }

    @Override
    public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
        return this.addDomHandler(handler, MouseMoveEvent.getType());
    }

}