2

I have a problem with getting setScrollTop to work in GWT. Basically, I'm trying to set a custom scroll position (to focus on a particular, desired element), right after the view is built. It doesn't seem to work because I don't set it at the correct time (and the UI might still be under rendering/construction, etc).

Setup is as follows.

  • I have a widget who's start method looks like this :

    public void start(AcceptsOneWidget widget, EventBus eventBus) {
    
         widget.setWidget(view);s
             Runnable onLoadCallback = new Runnable() {
             public void run() {
                 initView();
             }
         };
    
         VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);
    }
    
  • inside the initView panel, I dynamically build a view which contains some charts (by doing a request to the server, waiting for the answer to come, and then rendering the UI based on server's answer).

  • after the answer from the server arrives, and after I finished building the UI, I'm trying to set the scroll position like this :

    view.getContainerPanel().getElement().setScrollTop(2000);
    
  • this seems to have absolutely no effect whatsoever. HOWEVER, if I leave this view, and get back to it a bit later, it does work (presumably because it has already been initialized or something ?)

Question would be : what is the correct moment to call setScrollTop ? I tried overriding some of the methods from the corresponding view, but it doesn't seem to work.

Andrei
  • 1,613
  • 3
  • 16
  • 36

1 Answers1

5

Use a Scheduler after initView():

Scheduler.get().scheduleDeferred(new ScheduledCommand() {

    @Override
    public void execute() {
        view.getContainerPanel().getElement().setScrollTop(2000);
    }
});

Explanation:

Your program does not know how long it will take the browser to render the view. This is what Scheduler.scheduleDeferred() method was created for. It tells your program to wait until the browser has finished doing whatever it was doing and is ready for the next task.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Looks like it would work. However, it seems a bit hacky. Thanks anyway, but I'm looking for a more standard solution. – Andrei Sep 25 '12 at 14:49
  • 1
    This is the standard solution. Read this: http://stackoverflow.com/questions/5131086/using-the-gwt-scheduler – Andrei Volgin Sep 25 '12 at 14:52
  • I've read the article you pointed me to. I don't see why you say it indicates that this is the standard solution. Also, if I do this, there is a bit difficult to set the scroll just once (i.e. user manually scrolls, and you reset his scroll back to the original position). – Andrei Sep 25 '12 at 15:46
  • In support of the argument @AndreiVolgin makes, this is indeed the standard solution. Browsers calculate DOM element sizes during DOM tree attachment. The `scheduleDeferred()` method is there to postpone your logic until after the attachment happens. GWT itself [uses it](http://code.google.com/searchframe#T04cSGC7sWI/trunk/user/src/com/google/gwt/user/client/ui/ResizeLayoutPanel.java&q=ResizeLayoutPanel%20package:google-web-toolkit%5C.googlecode%5C.com&l=442) a lot. – Boris Brudnoy Sep 25 '12 at 15:48
  • Ok, thanks then. I'll give it a try and get back to you with an answer. – Andrei Sep 25 '12 at 16:00