2

PopupPanel在 GWT 中编写了我自己的。我想显示相对于其他小部件的弹出窗口。我的类的实现如下所示:

public class Popover extends PopupPanel implements PositionCallback {

    private static final Binder binder = GWT.create(Binder.class);
    private UIObject relative;

    interface Binder extends UiBinder<Widget, Popover> {
    }

    public Popover() {
        setWidget(binder.createAndBindUi(this));
        setStylePrimaryName("popover");
    }

    public void show(UIObject relative) {

        this.relative = relative;
        setPopupPositionAndShow(this);

    }

    public void setPosition(int offsetWidth, int offsetHeight) {

        if (relative != null) {

            int left = relative.getAbsoluteLeft();
            int top = relative.getAbsoluteTop();
            int width = relative.getOffsetWidth();
            int height = relative.getOffsetHeight();

            int topCenter = top + height / 2 - offsetHeight / 2;


            if (offsetWidth < left) {
                setPopupPosition(left - offsetWidth, topCenter);
            } else {
                setPopupPosition(left + width, topCenter);

            }
        }
    }

}

问题是,offsetWidth而且offsetHeight总是如此10

我的Popover.ui.xml样子如下:

<g:FlowPanel stylePrimaryName="popover">
    <g:FlowPanel stylePrimaryName="arrow" />
    <g:FlowPanel stylePrimaryName="inner">
        <g:Label stylePrimaryName="title" text="New Label" />
        <g:FlowPanel stylePrimaryName="content">
            <g:Label text="Hallo" />
        </g:FlowPanel>
    </g:FlowPanel>
</g:FlowPanel>
4

2 回答 2

6

为了解决这个问题,我遵循了PopupPanel's使用的步骤setPopupPositionAndShow(),但我将定位步骤设置为延迟命令。我的代码没有扩展PopupPanel,因此popupPanel变量:

popupPanel.setVisible(false);
popupPanel.show();

// Pausing until the event loop is clear appears to give the browser
// sufficient time to apply CSS styling.  We use the popup's offset
// width and height to calculate the display position, but those
// dimensions are not accurate until styling has been applied.
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
   @Override
   public void execute() {
      yourMethodToSetPositionGoesHere(popupPanel);
      popupPanel.setVisible(true);
   }
});
于 2013-02-07T07:28:38.707 回答
1

getOffsetWidth()并且getOffsetHeight()仅在 DOM 已完全构建并且您要为其获取值的容器可见且已附加时才有效。

你为什么不使用的showRelativeTo()功能PopupPanel

popup.showRelativeTo(SomeWidget);
于 2011-11-09T12:04:24.827 回答