positionscrollbarrefreshjfacetreeviewer

Setting scrollbar position of JFace TreeViewer


in my code I have a TreeViewer that is refreshed by periodically calling setInput() and passing the new input. Due to the complexity of the rest of the system I would prefer not to change the code to work with with add, remove and refresh. Unfortunately this means that the scrollbar jumps up to the top every second or so. Is there a way to preserve the scrollbar position and set it again after the refresh, even if no element is selected?

I already tried doing

int s = treeViewer.getTree().getVerticalBar().getSelection();
treeViewer.setInput(input); 
treeViewer.getTree().getVerticalBar().setSelection(s);

But I guess that would only work if an item was selected.


Solution

  • Nevermind, looks like only setting the input in the beginning and then calling refresh whenever previously the input was passed does the trick. Still don't know how to programmatically set the scrollbar, but apparently it's not required anymore, since simply using refresh did fix the error of jumping to the top.