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
Hi,

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.events.TreeAdapter;
import org.eclipse.swt.events.TreeEvent;
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;
    parent.setLayout(layout);

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

    tree2.getVerticalBar().setVisible(false);
    tree2.getVerticalBar().setEnabled(false);

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

    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);
        subItem
            .setText(new String[] { "subitem " + j});
      }
    }

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

    TreeColumn column11 = new TreeColumn(tree, SWT.LEFT);
    column11.setText("Column 1");
    column11.setWidth(0);
    column11.setResizable(false);
    TreeColumn column21 = new TreeColumn(tree, SWT.CENTER);
    column21.setText("Column 2");
    column21.setWidth(200);
    TreeColumn column31 = new TreeColumn(tree, SWT.RIGHT);
    column31.setText("Column 3");
    column31.setWidth(200);
    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);
        subItem
            .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);
    		tree.setSelection(tree2.getSelection());
    	}
    });

    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);
    		tree.setSelection(tree2.getSelection());
    	}
    });


    tree2.addListener(SWT.Selection, new Listener(){
    	public void handleEvent(Event event) {
    		tree.setSelection(tree2.getSelection());
    	}
    });

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


    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}


[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: 279
Registered: July 2009
Location: India
Senior Member
Hi,

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. http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org. eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet234 .java

HTH,
Lakshmi


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: Fri Mar 29 01:01:08 GMT 2024

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

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

Back to the top