Cod*_*ein 5 java swt jface scrollbar treeviewer
我将两个TreeViewers放入 SWT Shell。
他们每个人都有自己的滚动条。不过,我想只有一个Scrollbar在Shell其控制的滚动TreeViewers同时。
我能够找到的唯一示例是在源比较视图中。但是,我看不出他们是如何做到这一点的——我已经尝试弄明白了一段时间。但至少我知道这是可能的。
有什么建议?
编辑
我的最终界面应该在左边有两个TreeViewers和一个Scrollbar来控制它们。
这是一些应该对您有帮助的代码,但这不是解决方案。
我不知道如何添加一个模仿其他滚动条的附加滚动条。我所做的是将两者的滚动条运动互连起来ScolledComposites。也许这对你的努力有一点帮助......
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;
}
Run Code Online (Sandbox Code Playgroud)