GWT:PopupPanel setPopupPositionAndShow()提供了错误的offsetWidth和offsetHeight

时间:2011-11-08 17:12:08

标签: gwt popuppanel

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

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);

            }
        }
    }

}

问题是offsetWidthoffsetHeight始终是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>

2 个答案:

答案 0 :(得分:5)

要解决此问题,我按照PopupPanel的{​​{1}}使用的步骤进行了操作,但我将定位步骤作为延迟命令。我的代码没有扩展setPopupPositionAndShow(),因此PopupPanel变量:

popupPanel

答案 1 :(得分:1)

getOffsetWidth()getOffsetHeight()仅在DOM完全构造且您想要获取该值的容器可见且已附加时才起作用。

为什么不使用showRelativeTo()的<{1}}功能?

PopupPanel