简体   繁体   中英

How to find out which line was selected in one GWT grid canvas?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM