1

我有一个问题,我有一段时间自己无法解决。

我有一个包含两个部分的 RCP ViewPage。这些部分位于 SashForm 内,以便用户能够调整展开部分的大小。在底部有一个初始化后为空的树。通过用户交互(即删除过滤器),树被填满并包含大量数据。如果用户现在折叠底部视图并再次展开它,则树会调整大小,这会导致我的表单中出现 ScrollBars。我想要的是树视图中的滚动条。

以下是视图的构建方式:

- ScrolledForm
  - Form Body
    - Sash
      - Section 1
        - Composite
          - Some View
      - Section 2
        - Composite
          - Tree

我希望你明白我想要达到的目标。

更新:这是一些可以使用的源代码。它使用表而不是树,但会产生相同的问题。

public class MyPersonPageEditor extends FormPage {

    public static final String ID = "some.ID"; 

    TableViewer tableViewer;

    public MyPersonPageEditor(FormEditor editor) {
        super(editor, ID, "Some Title");
    }

    @Override
    protected void createFormContent(IManagedForm managedForm) {
        FormToolkit toolkit = managedForm.getToolkit();
        ScrolledForm form = managedForm.getForm();
        Composite formBody = form.getBody();
        formBody.setLayout(new GridLayout());

        form.setText("Some Title");
        toolkit.decorateFormHeading(form.getForm());

        SashForm sfForm = new SashForm(formBody, SWT.VERTICAL);
        sfForm.setLayout(new GridLayout());
        sfForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Section topSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
        topSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        topSection.setText("Section 1 Title");
        Composite topSectionComposite = toolkit.createComposite(topSection);
        topSectionComposite.setLayout(new GridLayout());
        toolkit.createLabel(topSectionComposite, "Just some content. Doesn't need to be much");
        Button btn = toolkit.createButton(topSectionComposite, "Create Table Content", SWT.PUSH);
        btn.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                for (int i = 0 ; i < 10 ; i++) {
                    tableViewer.add("Element " + i);
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {

            }
        });
        topSection.setClient(topSectionComposite);

        Section bottomSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
        bottomSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        bottomSection.setText("Section 2 Title");
        Composite bottomSectionComposite = toolkit.createComposite(bottomSection);
        bottomSectionComposite.setLayout(new GridLayout());
        bottomSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        bottomSection.setClient(bottomSectionComposite);
        Table table = toolkit.createTable(bottomSectionComposite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | 
                 SWT.V_SCROLL | SWT.H_SCROLL | SWT.RESIZE);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        table.setHeaderVisible(true);

        tableViewer = new TableViewer(table);
        tableViewer.add("New Element");
        new TableColumn(table, SWT.LEFT).setText("Spalte 1");

        TableLayout layoutDefault = new TableLayout();
        layoutDefault.addColumnData(new ColumnWeightData(1));
        table.setLayout(layoutDefault);

        form.reflow(true);
    }
}

如果您在启动后单击按钮,则表格看起来像左图。折叠并展开第二部分后,它看起来像正确的部分。

在此处输入图像描述

4

2 回答 2

1

这是有效的代码:您只需调整树/表的大小并确保它不会跨越垂直空间:

public void createPartControl(Composite parent) {

    FormToolkit toolkit = new FormToolkit(parent.getDisplay());
    final ScrolledForm form = toolkit.createScrolledForm(parent);
    Composite formBody = form.getBody();
    formBody.setLayout(new GridLayout());
    form.setText("Some Title");
    toolkit.decorateFormHeading(form.getForm());

    SashForm sfForm = new SashForm(formBody, SWT.VERTICAL);
    sfForm.setLayout(new GridLayout());
    sfForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    //top section
    Section topSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
    topSection.setLayout(new GridLayout());
    topSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    topSection.setText("Section 1 Title");
    Composite topSectionComposite = toolkit.createComposite(topSection);
    topSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    topSectionComposite.setLayout(new TableWrapLayout());

    toolkit.createLabel(topSectionComposite, "Just some content. Doesn't need to be much");
    Button btn = toolkit.createButton(topSectionComposite, "Create Table Content", SWT.PUSH);
    btn.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            for (int i = 0 ; i < 10 ; i++) {
                tableViewer.add("Element " + i);
            }
            form.reflow(true);
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {

        }
    });
    topSection.setClient(topSectionComposite);

    //bottom section
    Section bottomSection = new Section(sfForm, Section.TITLE_BAR | Section.EXPANDED | Section.TWISTIE);
    bottomSection.setLayout(new GridLayout());
    bottomSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    bottomSection.setText("Section 2 Title");
    Composite bottomSectionComposite = toolkit.createComposite(bottomSection);
    bottomSectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    bottomSectionComposite.setLayout(new TableWrapLayout());

    Table table = toolkit.createTable(bottomSectionComposite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | 
            SWT.V_SCROLL | SWT.H_SCROLL | SWT.RESIZE);

    TableWrapData ttd222 = new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.FILL_GRAB);
    ttd222.maxHeight =200;
    table.setLayoutData(ttd222);
    table.setHeaderVisible(true);

    tableViewer = new TableViewer(table);
    tableViewer.add("New Element");
    new TableColumn(table, SWT.LEFT).setText("Spalte 1");

    TableLayout layoutDefault = new TableLayout();
    layoutDefault.addColumnData(new ColumnWeightData(1));
    table.setLayout(layoutDefault);

    bottomSection.setClient(bottomSectionComposite);
    form.reflow(true);
}
于 2013-03-20T13:09:25.167 回答
0

- 确保 Tree 具有 SWT.H_SCROLL 样式(如果我没记错的话),即具有最小尺寸(例如,使用 GridLayout 和 GridData 将 minHeight 设置为 X 或使用 TableWrapLayout 和 TableData heightHint 设置为 X) - 如果你不这样做'无法让它工作只需告诉我,我会尝试制作代码;另外,带有布局的图片会很棒

于 2013-03-11T13:33:16.973 回答