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.
Use a Scheduler after initView():
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.