Tuesday, September 21, 2010

Why is getOffsetWidth/getOffsetHeight returning 0 even after onLoad is called for LayoutPanel children?

I am attempting to use the new GWT LayoutPanels and unfortunately they
are causing me some grief. We are using the layout panels
(RootLayoutPanel, DockLayoutPanel, LayoutPanel, etc) to arrange the
overall layout of the panel. Then, the children of one of these
panels needs to know how big it is so that it can size one of its
children properly to cause a scroll bar to appear. Generally you do
this by calling getOffsetWidth and getOffsetHeight. I know that these
methods will return 0 if the widget is not attached but I am finding
that in even putting the calls in onLoad, these methods are still
returning 0. Clearly I don't understand when GWT and/or the browser
figures out what the sizes of these layout panels are.

Below is a simplification of my problem. The widget added to the
RootLayoutPanel cannot determine its size when it is attached to the
DOM. I made sure the widget had something in it and to prove that it
ends up with a size, I added the call to the deferred command to
display the size again.

If you simply replace "RootLayoutPanel" with "RootPanel" then it will
output a size. What am I doing wrong here?

final FlowPanel testWidget = new FlowPanel() {
protected void onLoad() {
int width = getOffsetWidth();
System.out.println("width=" + width); // Outputs "0"
}
};
testWidget.add(new Label("Something"));

RootLayoutPanel.get().add(testWidget);

DeferredCommand.add(new Command() {
public void execute() {
int width = testWidget.getElement().getOffsetWidth();
System.out.println("width=" + width); // Outputs a non-zero
value
}
});

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment