首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使TreeViewer span ScrolledComposite

如何使TreeViewer span ScrolledComposite
EN

Stack Overflow用户
提问于 2016-12-08 15:41:39
回答 1查看 195关注 0票数 1

我有一个对话框,在n的左边有Tree的编号。每棵树都在一个ScrolledComposite中。

我的问题是,在滚动的组合中,没有树覆盖的空间是如此之多,我不知道如何删除它。

这是代码:

代码语言:javascript
复制
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;

public class SWTSashForm
{
 public static void main (String [] args) {
    Display display = new Display ();
    Shell shell = new Shell(display);

    shell.setLayout(new FillLayout());

    SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);


    Composite leftTablesComposite = new Composite(sashForm, SWT.NONE);
    leftTablesComposite.setLayout(new GridLayout());
    leftTablesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
    leftTablesComposite.setBackground(ColorConstants.white);


    for (int i = 0; i < 3; i++) {

        Label lbl = new Label(leftTablesComposite, SWT.NONE);
        lbl.setBackground(ColorConstants.white);
        lbl.setText("Tree " + i);

     // Configure scrolled composite
        ScrolledComposite scrolledComposite = new ScrolledComposite(leftTablesComposite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
        scrolledComposite.setLayout(new GridLayout());
        scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        scrolledComposite.setExpandVertical(true);
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setAlwaysShowScrollBars(true);
        scrolledComposite.setMinSize(leftTablesComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

        // Add content to scrolled composite
        Composite scrolledContent = new Composite(scrolledComposite, SWT.NONE);
        scrolledContent.setLayout(new GridLayout());
        GridData gridData = new GridData();
        gridData.horizontalAlignment = SWT.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.verticalAlignment = SWT.FILL;
        gridData.grabExcessVerticalSpace = true;
        scrolledContent.setLayoutData(gridData);
        scrolledContent.setBackground(ColorConstants.white);
        scrolledComposite.setContent(scrolledContent);

        TreeViewer tree = new TreeViewer(scrolledContent);
        for(int loopIndex0 = 0; loopIndex0 < 10; loopIndex0++) {
                TreeItem treeItem0 = new TreeItem(tree.getTree(), 0);
                treeItem0.setText("Level 0 Item "+ loopIndex0);

                for(int loopIndex1 = 0; loopIndex1 < 10; loopIndex1++) {
                    TreeItem treeItem1 = new TreeItem(treeItem0, 0);
                    treeItem1.setText("Level 1 Item "+ loopIndex1);

                    for(int loopIndex2 = 0; loopIndex2 < 10; loopIndex2++) {
                        TreeItem treeItem2 = new TreeItem(treeItem1, 0);
                        treeItem2.setText("Level 2 Item "+ loopIndex2);
                    }
                }
            }

    }
    new TreeViewer(sashForm);
    shell.open();

    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-08 15:50:54

添加这一行将告诉Tree填充空格:

代码语言:javascript
复制
tree.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41043294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档