Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 251
CodyBugstein Avatar asked Nov 03 '22 18:11

CodyBugstein


1 Answers

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();
like image 167
sambi reddy Avatar answered Nov 15 '22 04:11

sambi reddy