树不占用整个父组合

时间:2012-11-27 08:33:06

标签: java layout tree swt

我正在尝试在Tree内的Group合并中显示SWT SashForm。 SWT Tree在启动时没有填满整个Group组合,但是一旦我重新调整SashForm的大小,它就会这样做。我正在运行正常,即树正占据整个Group组合。谁能帮助我?

    sashForm = new SashForm(parent, SWT.HORIZONTAL | SWT.SMOOTH | SWT.H_SCROLL);
    sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

    explainStepsGroup = new Group(sashForm, SWT.NONE);
    explainStepsGroup.setText("Explain Steps");
    GridData gd = new GridData(SWT.FILL,SWT.FILL,true,true);
    explainStepsGroup.setLayoutData(gd);
    explainStepsGroup.setLayout(new FillLayout());

    tree = new Tree(explainStepsGroup, SWT.BORDER);
    tree.setLayout(new FillLayout());       
    tree.setHeaderVisible(true);
    tree.setToolTipText("Steps");
    ...
    ...
    void fillTreeItems() {  
    TreeItem rootTreeItem = new TreeItem(tree, SWT.NULL);
    rootTreeItem.setText("Steps");
    rootTreeItem.setExpanded(true); 

    TreeItem stepNumTreeItem = null;
    TreeItem stepTextItem = null;

    stepList = m_explainModel.getStepText();
    Iterator<String> iter = stepList.keySet().iterator();

    while(iter.hasNext()) {
    stepNumTreeItem = new TreeItem(rootTreeItem, SWT.NULL);
    String stepNum = iter.next();
    stepNumTreeItem.setText(stepNum.toString());
    stepNumTreeItem.setExpanded(false); 

    stepTextItem = new TreeItem(stepNumTreeItem, SWT.WRAP);
    stepTextItem.setText(stepList.get(stepNum));
    tree.showItem(stepNumTreeItem); 

    }
    tree.setRedraw(true);
    tree.pack();

}

2 个答案:

答案 0 :(得分:1)

这些问题通常很难弄明白。您需要回答的第一个问题是:哪个控件没有调整大小?

您认为它是树,但这只是因为您无法看到该组。所以也许树正在填充这个组,但是这个组没有填满窗扇。

要做的第一件事是给所有东西一个边框或一个独特的背景颜色。这样,您可以看到每个控件的位置以及它的大小。

这应该会给你一个想法。从代码中,这一行是无用的:

tree.setLayout(new FillLayout());

Tree使用它自己的固定布局来布置它的子节点。此外,您没有为SashForm设置布局,因此不会更改其子项的大小。尝试

sashForm.setLayout(new FillLayout());

as per this example

相关:Understanding Layouts in SWT - 旧但仍然是真的。

答案 1 :(得分:1)

您未在GridData上设置任何Tree。请尝试下面的示例代码:

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

        SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL | SWT.SMOOTH | SWT.H_SCROLL);
        sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Group firstGroup = new Group(sashForm, SWT.NONE);
        firstGroup.setText("Upper");
        firstGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        firstGroup.setLayout(new GridLayout(1, false));

        Tree tree = new Tree(firstGroup, SWT.BORDER);
        tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        tree.setHeaderVisible(true);
        tree.setToolTipText("Steps");

        fillTreeItems(tree);

        Group secondGroup = new Group(sashForm, SWT.NONE);
        secondGroup.setText("Lower");
        secondGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        secondGroup.setLayout(new GridLayout(1, false));

        Tree secondTree = new Tree(secondGroup, SWT.BORDER);
        secondTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        secondTree.setHeaderVisible(true);
        secondTree.setToolTipText("Steps");

        fillTreeItems(secondTree);

        shell.pack();
        shell.setSize(400, 200);
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    private static void fillTreeItems(Tree tree)
    {
        TreeItem rootTreeItem = new TreeItem(tree, SWT.NULL);
        rootTreeItem.setText("Steps");
        rootTreeItem.setExpanded(true);

        TreeItem item = new TreeItem(rootTreeItem, SWT.NULL);
        item.setText("item");
        item.setExpanded(false);

        tree.pack();
    }
}

这是它开始后的样子:

enter image description here

移动Sash后:

enter image description here