定位弹出GWT

时间:2012-07-04 08:42:53

标签: gwt

如何在PopT中的鼠标点击位置定位Popup。我尝试使用getX,并在flextable上设置getY并将Popup设置为该位置,但这不起作用。

1 个答案:

答案 0 :(得分:2)

快速举例:

final RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get();

final FocusPanel focusPanel = new FocusPanel();

/* just to make the clickable area visible */
focusPanel.getElement().getStyle().setBackgroundColor("red");

rootLayoutPanel.add(focusPanel);


final PopupPanel popupPanel = new PopupPanel();
popupPanel.add(new Label("Popup"));
popupPanel.show();

focusPanel.addClickHandler(new ClickHandler() {

  @Override
  public void onClick(final ClickEvent event) {
    final int left = event.getClientX();
    final int top = event.getClientY();

    popupPanel.setPopupPosition(left, top);
  }
});

注意:

  • 确保在添加focusPanel后显示PopupPanel,否则它将位于focusPanel后面。
  • 我在这里使用RootLayoutPanel,但您也可以相对于其他元素工作(使用left = event.getRelativeX(myContextElem);等。)
  • 如果您不想使用FocusPanel(具有ClickHandlers),您也可以使用MouseDownHandler,但在这种情况下,您需要调用sinkEvents(Event.ONMOUSEDOWN)