5

I'm putting two TreeViewers in an SWT Shell.

They each have their own scrollbars. However, I would like to have only one Scrollbar in the Shell which controls the scrolling of both TreeViewers simultaneously.

The only example of this I've been able to find is in the Source Compare View. However, I cannot see how they did this - I have been trying to figure it out for a while. But at least I know it's possible.

Any suggestions?

EDIT

My final interface should have two TreeViewers and one Scrollbar at the left to control them both.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • Do you mean that they both have a scrollbar, but moving one would move the other or do you want a single scrollbar somewhere that moves both composites? – Baz Jul 17 '13 at 17:24
  • They both have their own Scrollbar moving each of them independently. I would like to have one Scrollbar that moves both at the same time (both Trees are always the same size). (P.S. Glad to have you onboard @Baz :) – CodyBugstein Jul 17 '13 at 17:30
  • So you want 3 in total? – Baz Jul 17 '13 at 17:30
  • No, I'd like to have only one. One Scrollbar to rule them all. – CodyBugstein Jul 17 '13 at 17:45
  • If you eventually find a solution, please post it here. – Baz Jul 28 '13 at 07:48

2 Answers2

2

This should help you out. When i used SWT.NO_SCROLL for the Tree it stopping scrolling but Table, it hides vertical scroll bar and Table was scrolled when top index is set.

Tree

use setTopItem(TreeItem)-read documentation( as treeitem has child items )

Table

use setTopIndex(int index)

example:

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2,false));
final Tree tree1 = new Tree(shell, SWT.BORDER | SWT.MULTI /*| SWT.NO_SCROLL*/);

for (int i = 0; i < 128; i++) {
  TreeItem item = new TreeItem(tree1, SWT.NONE);
  item.setText("Tree 1 Item" + i);
}
GridData data = new GridData(SWT.FILL, SWT.FILL, false, false);
data.heightHint=200;
tree1.setLayoutData(data);



final Tree tree2 = new Tree(shell, SWT.BORDER | SWT.MULTI);
tree2.setSize(200, 200);
for (int i = 0; i < 128; i++) {
  TreeItem item = new TreeItem(tree2, SWT.NONE);
  item.setText("Tree 2 Item" + i);
}
tree2.getVerticalBar().addSelectionListener(new SelectionListener() {

  @Override
  public void widgetSelected(SelectionEvent e) {
    TreeItem item = tree2.getTopItem();
    int i = tree2.indexOf(item);
     item = tree1.getItem(i);
    tree1.setTopItem(item); 
  }

  @Override
  public void widgetDefaultSelected(SelectionEvent e) {
  }
});
data = new GridData(SWT.FILL, SWT.FILL, false, false);
data.heightHint=200;
tree2.setLayoutData(data);

shell.pack();
shell.open();
while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
    display.sleep();
}
display.dispose();
flavio.donze
  • 7,432
  • 9
  • 58
  • 91
sambi reddy
  • 3,065
  • 1
  • 13
  • 10
0

Here is some code that should help you, it's not a solution though.

I couldn't figure out how to add an additional scrollbar that imitates the others. What I did achieve, is to interlink the scrollbar movements of the two ScolledComposites. Maybe this helps you a little in your endeavour...

private static ScrolledComposite[] comps = new ScrolledComposite[2];

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

    createComposite(0, shell);
    createComposite(1, shell);

    shell.pack();
    shell.setSize(300, shell.getSize().y);
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

private static void createComposite(final int position, Shell parent)
{
     ScrolledComposite scrolled = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);

    Composite content = new Composite(scrolled, SWT.BORDER);
    content.setLayout(new GridLayout(3, true));

    for(int i = 0; i < 9; i++)
    {
        Button button = new Button(content, SWT.PUSH);
        button.setText("Button " + i);
    }

    scrolled.setExpandHorizontal(true);
    scrolled.setExpandVertical(true);
    scrolled.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    scrolled.setContent(content);

    scrolled.getHorizontalBar().addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            if(e.detail == SWT.DRAG)
            {
                ScrolledComposite source = comps[position];
                ScrolledComposite target = comps[(position + 1) % comps.length];
                int value = source.getHorizontalBar().getSelection();
                target.setOrigin(value, target.getOrigin().y);
            }
        }
    });

    comps[position] = scrolled;
}
Baz
  • 36,440
  • 11
  • 68
  • 94
  • The problem is I can't add TreeViewers to a ScrolledComposite – CodyBugstein Jul 17 '13 at 17:55
  • @Imray Why not? Mhm, can't you apply the same idea to `TreeViewer`s? – Baz Jul 17 '13 at 17:57
  • @Imray [This](http://stackoverflow.com/questions/8964914/vertical-align-2-jface-treeviewers-to-specific-element) might be helpful. – Baz Jul 17 '13 at 18:02
  • @Imray [This](http://stackoverflow.com/questions/8347737/scrolling-treeviewer-tree-swt) as well. – Baz Jul 17 '13 at 18:03