Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problems with scroll synchronization of 2 trees(problem with scroll synchronization of trees)
Problems with scroll synchronization of 2 trees [message #648095] Tue, 11 January 2011 08:40 Go to next message
No real name is currently offline No real nameFriend
Messages: 1
Registered: January 2011
Junior Member

I am developing an application and I need 2 synchronized trees in a part of it to show some data. The problem is that sometimes when expanding or collapsing a node the scroll in the trees gets out of sync. After using the scroll the trees will be back on sync.

Also, there is some weird behavior with the scroll bar in the right tree, it will show a position when we expand a node in the left one, but after moving the mouse over the second tree after expanding a node it will show the right position.

If anybody could give me a hint on how to fix the issue I would really appreciate it.

You can find next a snippet that shows the issue.

Thanks in advance for your help.

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

public class CreateTreeWithcolumns {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Composite parent = new Composite(shell, SWT.BORDER);

    GridLayout layout = new GridLayout(2, false);
    layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;

    final Tree tree2 = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.FULL_SELECTION);


    TreeColumn column1 = new TreeColumn(tree2, SWT.LEFT);
    column1.setText("Column 1");

    for (int i = 0; i < 10; i++) {
      TreeItem item = new TreeItem(tree2, SWT.NONE);
      item.setText(new String[] { "item " + i });
      for (int j = 0; j < 10; j++) {
        TreeItem subItem = new TreeItem(item, SWT.NONE);
            .setText(new String[] { "subitem " + j});

    final Tree tree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.FULL_SELECTION);

    TreeColumn column11 = new TreeColumn(tree, SWT.LEFT);
    column11.setText("Column 1");
    TreeColumn column21 = new TreeColumn(tree, SWT.CENTER);
    column21.setText("Column 2");
    TreeColumn column31 = new TreeColumn(tree, SWT.RIGHT);
    column31.setText("Column 3");
    for (int i = 0; i < 10; i++) {
      TreeItem item = new TreeItem(tree, SWT.NONE);
      item.setText(new String[] { "item " + i, "abc", "defghi" });
      for (int j = 0; j < 10; j++) {
        TreeItem subItem = new TreeItem(item, SWT.NONE);
            .setText(new String[] { "subitem " + j, "jklmnop",
                "qrs" });

    tree2.addTreeListener(new TreeAdapter() {
    	public void treeExpanded(TreeEvent evt) {
    		System.out.println("tree.treeExpanded, event="+evt);
    		//TODO add your code for tree.treeExpanded

    		tree.getItem(tree2.indexOf((TreeItem) evt.item)).setExpanded(true);

    tree2.addTreeListener(new TreeAdapter() {
    	public void treeCollapsed(TreeEvent evt) {
    		System.out.println("tree.treeCollapsed, event="+evt);
    		tree.getItem(tree2.indexOf((TreeItem) evt.item)).setExpanded(false);

    tree2.addListener(SWT.Selection, new Listener(){
    	public void handleEvent(Event event) {

    ScrollBar vBarLeft = tree2.getVerticalBar();
    vBarLeft.addListener(SWT.Selection, new Listener() {
    public void handleEvent(Event event) {
    ScrollBar vBarRight = tree.getVerticalBar();
    vBarRight.addListener(SWT.Selection, new Listener() {
    public void handleEvent(Event event) {

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {

[Updated on: Tue, 11 January 2011 09:58]

Report message to a moderator

Re: Problems with scroll synchronization of 2 trees [message #648165 is a reply to message #648095] Tue, 11 January 2011 14:40 Go to previous message
Lakshmi P ShanmugamFriend
Messages: 277
Registered: July 2009
Location: India
Senior Member

Here are a few things to try -
1) For the scrollbars to be synchronized after expansion/collapse you can use tree.setTopItem() in the TreeAdapter. You may have to call this inside display.async() so that it is called after the treeItem is expanded/collpased.

2) Get the index of selected item from tree2 and set the selection accordingly on tree1 instead of tree.setSelection(tree2.getSelection()). Same for tree2.setTopItem(tree.getTopItem())

3) Here is an example snippet which may be useful. It synchronizes scrolling and takes care of selection for 2 tables. eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet234 .java


Lakshmi P Shanmugam
Previous Topic:how to set a maximum width to a Text component ?
Next Topic:drag & drop of url link
Goto Forum:

Current Time: Thu Mar 04 17:06:25 GMT 2021

Powered by FUDForum. Page generated in 0.01815 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top