Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gwt getOffsetWidth returns zero

Tags:

gwt

I need to get the width of a widget after it has been rendered on the screen, because when I try to get its width after it is attached it returns zero. What is the earliest time I can retrieve the valid offsetwidth of a widget?

Thanks in advance!

like image 215
hba Avatar asked Oct 12 '11 22:10

hba


2 Answers

The offset width of the box only is known after the browser had built the content of the page. Use scheduleDefferred(), then you can get the value.

Example:

final TextBox yourInput = new TextBox();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {

  @Override
  public void execute() {
    int width = yourInput.getOffsetWidth();
    /* Do anything with width */
  }
});
like image 178
Johanna Avatar answered Oct 20 '22 21:10

Johanna


Take a look at Widget's onLoad() method. It is a no op method you can override, immeditely called after a widget becomes attached to the browser's document.

like image 1
pistolPanties Avatar answered Oct 20 '22 21:10

pistolPanties