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

1,118 views
Skip to first unread message

Damon Lundin

unread,
Sep 21, 2010, 5:13:22 PM9/21/10
to Google Web Toolkit
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
}
});

Gal Dolber

unread,
Sep 21, 2010, 6:21:55 PM9/21/10
to google-we...@googlegroups.com
try with onAttach()


--
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-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/




Thomas Broyer

unread,
Sep 21, 2010, 6:35:08 PM9/21/10
to Google Web Toolkit


On 21 sep, 23:13, Damon Lundin <damon.lun...@gmail.com> wrote:
> 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?

How about implementing RequiresResize and doing the job in onResize()?

Damon Lundin

unread,
Sep 21, 2010, 6:43:41 PM9/21/10
to Google Web Toolkit
On Sep 21, 5:21 pm, Gal Dolber <gal.dol...@gmail.com> wrote:
> try with onAttach()

The method onLoad is called by onAttach so overiding onAttach won't
change anything not to mention the fact that the doc for onAttach says
"It is strongly recommended that you override {@link #onLoad()} or
{@link #doAttachChildren()} instead of this method".

On Sep 21, 5:35 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> How about implementing RequiresResize and doing the job in onResize()?

That doesn't help because onResize isn't called automatically when the
widgets are initially constructed, only when a browser initiates a
resize event. However I have tried exactly what you suggest by
putting the code I am having trouble with in a RequiresResize.onResize
method and am calling that method in onLoad (so the sizing happens on
both resize and when the widget is initially created). The method
getOffsetWidth still returns 0. I also tried manually calling
onResize on the RootLayoutPanel at the end of my onModuleLoad with the
same result. Only a DeferredCommand seems to result in getOffsetWidth
not returning 0.

Didier DURAND

unread,
Sep 21, 2010, 11:28:22 PM9/21/10
to Google Web Toolkit
Did you check if you have the issue on all browsers: I have it on
Webkit based browsers (Safari + Chrome) but not on IE and FF.

didier

Damon Lundin

unread,
Sep 22, 2010, 11:28:38 AM9/22/10
to Google Web Toolkit
On Sep 21, 10:28 pm, Didier DURAND <durand.did...@gmail.com> wrote:
> Did you check if you have the issue on all browsers: I have it on
> Webkit based browsers (Safari + Chrome) but not on IE and FF.

I was remiss in leaving that info out. I am having this problem on
both IE 6 and FF 3.6. Although IE 6 doesn't return 0 for the first
call but rather 60 which is the width of the label. It does return
the full window width in the second call like FF. So you're saying
that you see different behavior than I am on IE and FF?

MIKE

unread,
Jul 4, 2012, 3:58:10 AM7/4/12
to google-we...@googlegroups.com
after updating chrome and FF to latest version, I am having the same problem.
Reply all
Reply to author
Forward
0 new messages