让FillLayout正常运行

时间:2014-06-12 13:52:40

标签: java user-interface layout swt grid-layout

我正在尝试创建一个占用整个显示的两棵树的视图。截至目前,这两棵树只占了 canvas 的一半(左半部分)。我无法弄清楚为什么,因为我已经尝试了许多不同的参数发送到每个设置方法,如setLayout,SashForm等。这是我的代码,我附加了一个图像,以显示我现在得到的简化视图是

    super(parent);
    setDisplayName("Viewer");
    setLayout(new FillLayout());
    ((FillLayout) getLayout()).marginHeight = ((FillLayout) getLayout()).marginWidth = 20;

    fullForm = new SashForm(this, SWT.VERTICAL);
    fullForm.setLayout(new FillLayout());

    adForm = new SashForm(fullForm, SWT.VERTICAL);
    adForm.setLayout(new FillLayout());

    countLabel = GUIToolkit.newLabel(this, SWT.CENTER, "", new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
    countLabel.setFont(boldFont);

    ad1Tree = createTree(fullForm, "name1", "col1", "col2");
    ad2Tree = createTree(fullForm, "name2", "col1", "col2");

创建树

    private static Tree createTree(final Composite parent, final String text, final String... columns) {
    final Composite group = GUIToolkit.newGroup(parent, SWT.NONE, text, null);
    group.setFont(FontManager.NORMAL_BOLD);
    group.setLayout(new FillLayout());
    final Tree tree = new Tree(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    tree.setHeaderVisible(true);
    GUIToolkit.createColumns(tree, columns);
    GUIToolkit.addColumnSort(tree, DATA);
    GUIToolkit.removeDoubleClickExpand(tree);

This is what the view looks like in a very simplified manner

1 个答案:

答案 0 :(得分:1)

我认为您的问题是,您没有为包含GridDataGroup本身的Tree分配Tree

尝试更新您的代码:

private static Tree createTree(final Composite parent, final String text, final String... columns) {
    final Composite group = GUIToolkit.newGroup(parent, SWT.NONE, text, new GridData(SWT.FILL, SWT.FILL, true, true)); // <-- THIS
    group.setFont(FontManager.NORMAL_BOLD);
    group.setLayout(new FillLayout());
    final Tree tree = new Tree(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // <-- THIS
    tree.setHeaderVisible(true);
    GUIToolkit.createColumns(tree, columns);
    GUIToolkit.addColumnSort(tree, DATA);
    GUIToolkit.removeDoubleClickExpand(tree);
}

不确定GUIToolkit到底是什么,但我假设它只会应用您提供的LayoutData


<强>更新

好的,我就是这样做的:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    Group top = new Group(shell, SWT.NONE);
    top.setLayout(new GridLayout(1, false));
    top.setText("Top");
    top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Group bottom = new Group(shell, SWT.NONE);
    bottom.setLayout(new GridLayout(1, false));
    bottom.setText("Bottom");
    bottom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Tree topTree = new Tree(top, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    createColumns(topTree);
    Tree bottomTree = new Tree(bottom, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    createColumns(bottomTree);

    shell.pack();
    shell.open();

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

private static void createColumns(Tree tree)
{
    tree.setHeaderVisible(true);

    for(int i = 0; i < 3; i++)
        new TreeColumn(tree, SWT.NONE).setText("Col " + i);

    for(int i = 0; i < 10; i++)
    {
        TreeItem item = new TreeItem(tree, SWT.NONE);

        for(int j = 0; j < tree.getColumnCount(); j++)
        {
            item.setText(j, "Item " + i + " " + j);
        }
    }

    for(int i = 0; i < tree.getColumnCount(); i++)
        tree.getColumn(i).pack();
}