1

I have a couple of GWT widgets which are displayed in a view.

The view contains content like this:

FlowPanel mainPanel = new FlowPanel();
RootPanel.get().add(mainPanel);
Label label = new Label("test");
mainPanel.add(label);
FlowPanel otherPanel = new FlowPanel();
mainPanel.add(otherPanel);

The mainPanel gets its final height after the view has been fully rendered. I need to get the value of height from the mainPanel after the render process is completed.

Here is what I do so far:

    new Timer() {

        @Override
        public void run() {
            int height = $(mainPanel).height();
            // do something with the mainPanel height
        }
    }.schedule(300);    

Noite: $(mainPanel) is the use of GWTQuery. I set a timer to hope that the view render process is completed when the timer fires. I suppose this is not a very clever solution.

How can I detect if the view is fully rendered in order to get the final height of the mainPanel?

Edit:

I also tried to use:

@Override
protected void onReveal() {
    super.onReveal();
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

        @Override
        public void execute() {
            // get mainPanel height is incorrect
        }
    });
}

but it seems that the rendering was not completed so the mainPanel did not have the correct height.

Edit:

It seems that the Scheduler works different on mobile and desktop when using SDM. I created a demo project (https://github.com/confile/GWT-2.7-Scheduler-Test) to show the problem.

I created another question for this issue here: https://stackoverflow.com/questions/27128787/gwt-2-7-scheduler-works-different-on-mobile-and-desktop-in-sdm

Community
  • 1
  • 1
Michael
  • 32,527
  • 49
  • 210
  • 370

3 Answers3

1

You should use a Scheduler, not Timer. See GWT: Timer and Scheduler Classes, which explains the distinction.

UPDATE:

Also, what is $(mainPanel)? You should do:

 int height = mainPanel.getOffsetHeight();
Community
  • 1
  • 1
Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Please see my edit. I tried Scheduler but it seems that the rendering is not finished when scheduler deferred is executed. – Michael Nov 24 '14 at 18:54
  • That's impossible. Browsers only have one thread - they cannot execute code in parallel. Anyway, you have a different problem - I updated my answer. – Andrei Volgin Nov 24 '14 at 19:29
  • @$(mainPanel) is the use of [GWTQuery][1]. Which Scheduler method would you use in my case? – Michael Nov 24 '14 at 20:12
  • If `mainPanel.getOffsetHeight()` returns zero in `Scheduler.get().scheduleDeferred`, there is some other problem in your code. – Andrei Volgin Nov 24 '14 at 20:24
  • I use GWTP and when I use the onReveal() method (see my edit) then the mainPanel is not zero but is is also not the final size. – Michael Nov 24 '14 at 21:07
  • Okay I think there is a problem in GWT SDM the Scheduler works different on desktop and mobile here is a demo project for that: https://github.com/confile/GWT-2.7-Scheduler-Test – Michael Nov 25 '14 at 13:43
1

Gwt itself gives you a hook to do it. It is onLoad(). You can override it.

Example. If you want to know when widgets of a FlowPanel are rendered, you can use

FlowPanel flowPanel = new FlowPanel(){
                      @Override
                      protected void onLoad()
                      {
                        //Do your stuff here
                      }};
Abhijith Nagaraja
  • 3,370
  • 6
  • 27
  • 55
-1

This was posted in How to detect if a page has fully rendered using jQuery?

$(window).load(function() {
    //everything is loaded
});
Community
  • 1
  • 1
Jack Shultz
  • 2,031
  • 2
  • 30
  • 53