What do you call "getCellForEvent ()" for all GWT Grid mouse events?

For GWT, when you have a Grid, I want to receive events for which row and column.

So, I expanded the GWT grid to add mouse events:

@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()); } 

Then I register an event handler for the mouse click:

 // 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()+")"); } }); 

So, now I get events for specific cells ... but only when I click. What do you call "getCellForEvent ()" for all mouse events?

+4
source share
2 answers

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 { /** * 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()); } } 

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!

+3
source
 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; } } } 

Try this one

+1
source

All Articles