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
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.
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.