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

I put two TreeViewers in a SWT Shell .

Each of them has its own scrollbars. However, I would like to have only one Scrollbar in the Shell , which simultaneously controls the scrolling of both TreeViewers .

The only example I could find is in the Source Compare View. However, I do not see how they did it - I tried to figure it out for a while. But at least I know this is possible.

Any suggestions?

EDIT

My last interface is to have two TreeViewers and one Scrollbar on the left to manage them.

+4
source share
2 answers

This will help you. When I used SWT.NO_SCROLL for Tree to stop scrolling, but Table , it hides the vertical scroll bar and the table was scrolled when superscript is set.

Wood

use setTopItem(TreeItem) -read documentation (since treeitem has children)

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(); 
+2
source

Here is some code that should help you, this is not a solution.

I could not figure out how to add an extra scrollbar that mimics the rest. What I have achieved is to link the scrollbar movements of two ScolledComposites . Maybe this will help you a little in your efforts ...

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

Source: https://habr.com/ru/post/1491939/


All Articles