猿问

纯 GWT,事件转换

因此,我在 GWT 中有一个“FlexTable”,我需要在右键单击某个单元格时下拉操作列表。在鼠标左键单击以检索我的单元格的“rowIndex”时,我只需使用 ClickEvent 方法“getCellForEvent(event).getRowIndex()”。但是纯 GWT 中没有右键单击处理程序。所以我决定使用需要 ContextMenuEvent 的 ContextMenuHandler。当然,我不能将 ContextMenuEvent 放入 ClickEvent 方法“getCellForEvent”中。这种情况有什么解决办法吗?或者也许有人知道在右键单击“FlexTable”时下拉列表的更简单方法。



千巷猫影
浏览 68回答 3
3回答

慕码人8056858

我已经为 CellTable 或 DataGrid 小部件完成了此操作,但没有为 FlexTable 完成此操作。对于前者,我将一个处理程序对象应用于整个网格小部件,并使用该事件计算出事件发生的行或单元格。我看不出如何使用 FlexTable 完成此操作。使用 FlexTable 时,一个技巧是为每个单元格创建一个处理程序对象,并在创建时告诉它单元格/行。是这样的:&nbsp; &nbsp; cell.addDomHandler(new ContextMenuHandler() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onContextMenu(ContextMenuEvent event)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // stop the browser from opening the context menu&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.stopPropagation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeEvent nativeClickEvent = event.getNativeEvent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; displayPopupMenuForCell(cell, nativeClickEvent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, ContextMenuEvent.getType());在上面,cell需要是一个Widget。因此,您需要获取 HTMLTable 中的元素(FlexTable 扩展了 HTMLTable,这是一个普通的 HTML 表格元素)并将其包装为一个 Widget。我不确定该怎么做,但这是可能的。另一件事,您需要防止浏览器弹出自己的上下文菜单。我将其添加到body标签中的 html 文件中:&nbsp; <body oncontextmenu="return false" >

Helenr

实际上你可以使用点击处理程序来检查点击了哪个按钮,左,右或中间你需要做的是这样的:Button button= new Button();button.addClickHandler(event -> {&nbsp; &nbsp; NativeEvent nativeEvent = event.getNativeEvent();&nbsp; &nbsp; if(NativeEvent.BUTTON_RIGHT == nativeEvent.getButton()){&nbsp; &nbsp; &nbsp; &nbsp; event.stopPropagation();&nbsp; &nbsp; &nbsp; &nbsp; //do something&nbsp; &nbsp; }});

慕少森

如果您使用 CellTable 或 DataGrid Widget,您可以处理CellPreviewEvent单击单元格时触发的 ,它还可以告诉您事件发生在哪一行/单元格中。这是一些代码:private void makeItemRightClickListener(){&nbsp; &nbsp; grid.addCellPreviewHandler(new CellPreviewEvent.Handler<T>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onCellPreview(CellPreviewEvent<T> event)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (event.getNativeEvent().getButton() !=&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeEvent.BUTTON_RIGHT)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Prevent browser's own context menu from appearing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.getNativeEvent().preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.getNativeEvent().stopPropagation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleItemRightClickEvent(event);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}private void handleItemRightClickEvent(CellPreviewEvent<T> event){&nbsp; &nbsp; NativeEvent nativeClickEvent = event.getNativeEvent();&nbsp; &nbsp; // Get the data (type T) that is being displayed in the cell&nbsp; &nbsp; // by the CellTable or DataGrid widget.&nbsp; &nbsp; T rowClicked = event.getValue();&nbsp; &nbsp; // Create PopupPanel for menu&nbsp; &nbsp; PopupPanel popup = ...&nbsp; &nbsp; // Show the popup menu at the click position&nbsp; &nbsp; UIObject target = new MousePointUIObject(nativelickEvent);&nbsp; &nbsp; popup.showRelativeTo(target);}private static class MousePointUIObject extends UIObject{&nbsp; &nbsp; private NativeEvent mouseEvent;&nbsp; &nbsp; public MousePointUIObject(NativeEvent mouseEvent)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.mouseEvent = mouseEvent;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getAbsoluteLeft()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return mouseEvent.getClientX() + Window.getScrollLeft();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getAbsoluteTop()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return mouseEvent.getClientY() + Window.getScrollTop();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getOffsetWidth()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getOffsetHeight()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答