I have a getCellForEvent () function that works for all types of mouse events (and not just clicks).
To get the desired behavior, increase the gwt grid. Then paste where getCellForEvent () is located, and copy this bit of code into the new extended grid. Change getCellForEvent () from "ClickEvent" to "MouseEvent".
onModuleLoad ():
/*------ Create the main grid display ------*/ // grid object final GridWithMouse g = new GridWithMouse(2, 2); // mouse over event handler g.addMouseOverHandler(new MouseOverHandler() { @Override public void onMouseOver(MouseOverEvent event) { Cell cell = ((GridWithMouse)event.getSource()).getCellForEvent(event); if(cell!=null) { System.out.println("Over: ("+cell.getRowIndex()+","+cell.getCellIndex()+")"); } } }); // mouse move event handler g.addMouseMoveHandler(new MouseMoveHandler() { @Override public void onMouseMove(MouseMoveEvent event) { Cell cell = ((GridWithMouse)event.getSource()).getCellForEvent(event); if(cell!=null) { System.out.println("Move: ("+cell.getRowIndex()+","+cell.getCellIndex()+")"); } } }); // mouse click event handler g.addClickHandler(new ClickHandler() { @SuppressWarnings("rawtypes") @Override public void onClick(ClickEvent event) { Cell cell = ((GridWithMouse)event.getSource()).getCellForEvent((MouseEvent)event); if(cell!=null) { System.out.println("Click: ("+cell.getRowIndex()+","+cell.getCellIndex()+")"); } } });
New extended Grid class:
package 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 { 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)); } } } @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()); } }
Now, instead of getting pixel locations for all mouse events, you get grid locations for all mouse events ... what Grid really needs from the start!
source share