0

I'm working on one GWT grid Canvas like this in the fiddle

My gridCanvas Object has horizontal lines represent paths. Also the object has vertical lines that represent sections.

I want to know how I could get the user's selection, for example, if the user has selected a route or a section and which route or section is selected.

Here a screenshot of case.

Loktar
  • 34,764
  • 7
  • 90
  • 104
user3410517
  • 295
  • 3
  • 18

1 Answers1

0

Add the event you want to listen to like gridCanvas.addClickHandler(ClickHandler) and use the ClickEvent to obtain the mouse position and calculate the cell the user clicked on.

gridCanvas.addClickHandler(new ClickHandler() {
   @Override
   public void onClick(ClickEvent event) {
       int row = event.getY() / getCellHeight();
       int col = event.getX() / getCellWidth();
   }
});

With getX() and getY(), you obtain the position of the mouse. Since you constructed the grid, you should also have the width and height of a cell. With this information, it is then easy to get the row and/or column.

I made a full sample that you can find here.

The sample is deployed here.

Nicolas Morel
  • 315
  • 2
  • 8
  • Very nice your example. In my case i need select the lines not the cells. I'actualized the question with one screenshot – user3410517 Jul 17 '14 at 08:49
  • You could generate a Map for horizontal lines and vertical lines when you construct the grid. The key is the x/y position, the value the index or any object that represents your line. And then use the `event.getX()`and `event.getY()` to find the line – Nicolas Morel Jul 17 '14 at 09:20
  • If your lines are larger than 1px or if you want your user to select the line even if he click 1px above, you'll need a more complex approach. A bit like a Map but where the key is a range and the equality is based on intersection. – Nicolas Morel Jul 17 '14 at 09:26