Documention suggestion for Widget#getOffsetHeight

10 views
Skip to first unread message

jan...@gmail.com

unread,
May 24, 2006, 6:10:27 PM5/24/06
to Google Web Toolkit
Hi,

Using the class below I found that the offsetHeight property of a
Button is 0 before it is added to a Panel. This makes sense because the
styles that will apply to the Button are not known before it is added.
Maybe this behaviour could be added to the JavaDoc?

This is my test class,

public class OffsetHeightExploration implements EntryPoint {

/**
* This is the entry point method.
*/
public void onModuleLoad() {
final Button button = new Button("A button");
final Label label1 = new Label();
final Label label2 = new Label();
label1.setText("Before adding to panel: offsetWidth of button: " +
button.getOffsetWidth()
+ ", offsetHeight of button: " + button.getOffsetHeight());

RootPanel.get("slot1").add(button);
RootPanel.get("slot2").add(label1);
RootPanel.get("slot2").add(label2);

label2.setText("After adding to panel: offsetWidth of button: " +
button.getOffsetWidth()
+ ", offsetHeight of button: " + button.getOffsetHeight());
}
}

and this is output in the browser,

Before adding to panel: offsetWidth of button: 0, offsetHeight of
button: 0
After adding to panel: offsetWidth of button: 78, offsetHeight of
button: 23

Regards,
Janek Bogucki

john.th...@gmail.com

unread,
May 24, 2006, 7:10:44 PM5/24/06
to Google Web Toolkit
I struck exactly the same problem this morning trying to write a
graphics button...

final Button button = new Button("<IMG SRC=\"image.gif\">");
Image buttonImage = new Image("image.gif");
String width = Long.toString(buttonImage.getOffsetWidth());
String height = Long.toString(buttonImage.getOffsetHeight());

The button never gets displayed because the width and height are zero
at this stage! :-o

Wondering how to dynamically get the image size and size the button
appropriately...

John.

j...@google.com

unread,
May 25, 2006, 10:50:59 AM5/25/06
to Google Web Toolkit
This characterization of offset[Width|Height] is correct -- browsers do
not measure elements until they are actually connected to the DOM (this
is actually strictly necessary because its position can affect its size
due to CSS). We can definitely make this more clear in the
documentation.

As for the image button case, I believe this code should work properly.
If you read an image's size before it is attached (and loaded), it
will likely return 0. However, the button with an image within it
should automatically re-layout as soon as the image is loaded. I ran
the following test, which seems to do the right thing:

public class ImageButtonTest implements EntryPoint {
public void onModuleLoad() {


Button button = new Button("<IMG

SRC='http://www.google.com/images/logo.gif'>");
RootPanel.get().add(button);

int width = button.getOffsetWidth();
int height = button.getOffsetHeight();
Window.alert("" + width + "," + height);
}
}

Do note that the width/height measurement might not come out right
immediately, as we're not waiting on the button's image to load. The
button does, however, re-layout as soon as the image does load.

Hope this helps!
joel.

Reply all
Reply to author
Forward
0 new messages