Gwt getOffsetWidth returns zero

I need to get the width of the widget after it has been displayed on the screen, because when I try to get its width after attaching it, it returns zero. What early time can I get the actual offset width of the widget?

Thanks in advance!

+7
source share
3 answers

The field offset width is only known after the browser has built the contents 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 */ } }); 
+5
source

Take a look at the Widget onLoad () method. This is the no op method, which you can override, immediately called after attaching the widget to the browser document.

+1
source

Try a combination of onLoad () and scheduleFinally ().

In the code below, the onResize and "scheduleFinally" callbacks will always have good results. Between the two of them you must catch all the necessary events.

"onLoad" often returns 0 for size, as described in the original task. You can just skip this part.

  @Override public void onResize() { recordSize("onResize"); } @Override public void onLoad() { super.onLoad(); recordSize("onLoad"); Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() { @Override public void execute() { recordSize("scheduleFinally"); }}); } 
+1
source

All Articles