CodyBugstein
CodyBugstein

Reputation: 23322

SWT: How to use only one scrollbar to scroll two seaparate viewers

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.

Upvotes: 5

Views: 1486

Answers (2)

sambi reddy
sambi reddy

Reputation: 3085

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();

Upvotes: 2

Baz
Baz

Reputation: 36894

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;
}

Upvotes: 0

Related Questions