Search code examples
gwt2

GWT popup is not centered when built within onClickHandler


My aim is to use GWT.runSync to load the popup contents only when required.

If I construct my widget as:

public class CreateButton extends Button {

public CreateButton() {
    super("Create");
    buildUI();
}

private void buildUI() {

    final CreateWidget createWidget = new CreateWidget();

    final PopupPanel popupPanel = new PopupPanel(false);
    popupPanel.setWidget(createWidget);
    popupPanel.setGlassEnabled(true);
    popupPanel.setAnimationEnabled(true);
    addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            popupPanel.center();

        }
    });
}
}

Then the popup will be centered correctly.

If I build the popup within the clickHandler:

public class CreateButton extends Button {

public CreateButton() {
    super("Create");
    buildUI();
}

private void buildUI() {

        @Override
        public void onClick(ClickEvent event) {
            final CreateWidget createWidget = new CreateWidget();

            final PopupPanel popupPanel = new PopupPanel(false);
            popupPanel.setWidget(createWidget);
            popupPanel.setGlassEnabled(true);
            popupPanel.setAnimationEnabled(true);
            addClickHandler(new ClickHandler() {

            popupPanel.center();

        }
    });

}
}

The popup will not center correctly. I have tried using setPositionAndShow, however the supplied offsets are 12, even though the CreateWidget is actually about 200px for both width and height.

I want to use the second method so I can eventually use GWT.runAsync within the onClick as CreateWidget is very complex.

I am using GWT-2.1.1


Solution

  • Seems to work by delaying the call to center. Perhaps a once off Timer would work as well. Delaying the call also works when wrapping buildUI within GWT.runAsync

    public class CreateButton extends Button {
    
        public CreateButton() {
            super("Create");
            buildUI();
        }
    
        private void buildUI() {
    
            @Override
            public void onClick(ClickEvent event) {
                final CreateWidget createWidget = new CreateWidget();
    
                final PopupPanel popupPanel = new PopupPanel(false);
                popupPanel.setWidget(createWidget);
                popupPanel.setGlassEnabled(true);
                popupPanel.setAnimationEnabled(true);
                addClickHandler(new ClickHandler() {
    
                    Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
    
                        @Override
                        public boolean execute() {
    
                            popupPanel.center();
                            return false;
    
                        }
                    }, 50); //a value greater than 50 maybe needed here.
                });    
            }
        }
    
    }
    }