Search code examples
gwtgwt-2.2-celltable

GWT: How to center element containing CellTable?


I'm trying to center an element that contains a CellTable. The actual centering logic works okay, but I'm having problems with all those attaching/detaching events. Basically, I'm doing this in my container widget:

@Override
public void onLoad() {
  super.onLoad();
  center();
}

However, it seems that onLoad on the container does not mean that all children have loaded, so... the actual centering routine is called too early and Element.getOffsetWidth/getOffsetHeight are both returning 0. This results in the container being displayed with the left upper corner in the center of the screen.

Same thing happens if I use an AttachEvent.Handler on the CellTable.

So... is there any event on CellTable, or on Widget or whatever that allows me to trigger an action when the DOM subtree has been attached to the DOM?

Thanks in advance.


Solution

  • Take a look at scheduleDeferred. A deferred command is executed after the browser event loop returns.

    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
        @Override
        public void execute() {
            center();
        }
    });