SWT在ScrolledComposite中设置组件的最大高度

时间:2015-01-14 15:01:38

标签: java swt

我无法弄清楚如何正确设置行复合的高度。我想以下代码片段只显示一行数据。使用滚动条可以看到任何其他行。我尝试过使用rowContainer.setSize()但无济于事。任何帮助,将不胜感激。

请注意,滚动的复合材料不直接链接到rowContainer复合材料,也包含其他复合材料。

private void doStuff(final Composite parentContainer) {
    final ScrolledComposite scrolledComposite = new ScrolledComposite(
        parentContainer, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setExpandVertical(true);
    scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, 
        true, true));

    final Composite borderComposite = new Composite(scrolledComposite, 
        SWT.BORDER);
    borderComposite.setLayout(new GridLayout(1, false));
    borderComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, 
        true));
    scrolledComposite.setContent(borderComposite);
    scrolledComposite.setSize(borderComposite.computeSize(SWT.DEFAULT, 
        SWT.DEFAULT));

    // Create a container for a label and button
    final Composite labelAndAddButtonContainer = new Composite(
        borderComposite, SWT.NONE);
    labelAndAddButtonContainer.setLayout(new GridLayout(2, false));
    labelAndAddButtonContainer.setLayoutData(new GridData(SWT.LEFT, 
        SWT.TOP, false, false));

    // Create a container to hold all dynamic rows
    final Composite rowContainer = new Composite(borderComposite, SWT.NONE);
    rowContainer.setLayout(new GridLayout(1, false));
    rowContainer.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

    // Create at least one row. 
    final Collection<String> rowData = getRowData();
    if (rowData.isEmpty()) {
        createRow(scrolledComposite, rowContainer, "");
    } else {
        for (final String text : rowData) {
            createRow(scrolledComposite, rowContainer, text);
        }
        // How do I adjust for the height so that only 1 row shows, 
        // and scrollbars appear if there is more than one row?
    }

    // Create a button
    final Button addButton = new Button(labelAndAddButtonContainer, 
        SWT.PUSH);
    addButton.setImage(someImage);

    final Label label = new Label(labelAndAddButtonContainer, SWT.NONE);
    label.setText("SOME_LABEL");
}

0 个答案:

没有答案