3

I have a FlowPanel, whose height is fixed (it is actually a percentage of its parent's height, which is fixed).
In this panel, I add several div elements. Using CSS, I set its height as 100% of its parent.
What I want to do is set its width to be equal to its height using javascript.

The issue I have is that I don't know when to run this bit of javascript. I tried adding it to the onLoad() method of my container, but the height is not known yet (getOffsetHeight() returns 0).

I had a look at similar questions (like GWT - Retrieve size of a widget that is not displayed), but they were not exactly the same thing.

Community
  • 1
  • 1
Sébastien Tromp
  • 603
  • 1
  • 15
  • 30

1 Answers1

7

Defer the call to getOffsetHeight() until the end of the event loop:

@Override
public void onLoad() {
  Scheduler.get().scheduleDeferred(new ScheduledCommand() {
    @Override
    public void execute() {
      // Get and work with the element's offsetHeight.
    }
  }
}
Jason Terk
  • 6,005
  • 1
  • 27
  • 31
  • It worked perfectly. I didn't know anything about the Scheduler, and it looks like something I will be using now and then. A big thanks for this :) – Sébastien Tromp Nov 05 '11 at 08:13
  • @Jason Terk I couldn't get this work correctly in GWTP: http://stackoverflow.com/questions/27108858/how-to-detect-if-a-view-is-fully-rendered – Michael Nov 24 '14 at 21:09