JFace ColumnWeigthData导致父级增长 [英] JFace ColumnWeigthData causes parent to grow

查看:178
本文介绍了JFace ColumnWeigthData导致父级增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Eclipse RCP应用程序,并且想在TableViewer中使用动态列大小,并使用ColumnWeigthData作为ColumnLayoutData.问题在于,每当我对表格进行布局时,父表单(示例代码中的ScrolledForm)都会增加几个像素.

I got an Eclipse RCP app and want to use dynamic column size in a TableViewer using ColumnWeigthData as ColumnLayoutData. Problem is that the parent form (ScrolledForm in example code) grows a few pixels whenever I layout the table.

要重现,您可以运行该示例并多次打开/关闭该部分.每次关闭时,该区域都会变宽.

To reproduce you can run the example and open/close the Section a few times. On every close the section gets wider.

为什么要这样做,如何停止?

Why does it do this and how can I make it stop?

package com.test;

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
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.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;

public class TestShell extends Shell {
    private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
    private final Table table;

    public static void main(final String args[]) {
        try {
            final Display display = Display.getDefault();
            final TestShell shell = new TestShell(display);
            shell.open();
            shell.layout();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }

    public TestShell(final Display display) {
        super(display, SWT.SHELL_TRIM);
        setLayout(new FillLayout(SWT.HORIZONTAL));

        final ScrolledForm scrldfrmNewScrolledform = formToolkit.createScrolledForm(this);
        formToolkit.paintBordersFor(scrldfrmNewScrolledform);
        scrldfrmNewScrolledform.setText("New ScrolledForm");
        scrldfrmNewScrolledform.getBody().setLayout(new GridLayout(1, false));

        final Section sctnSection =
                formToolkit.createSection(scrldfrmNewScrolledform.getBody(), Section.TWISTIE | Section.TITLE_BAR);
        sctnSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        formToolkit.paintBordersFor(sctnSection);
        sctnSection.setText("Section");
        sctnSection.setExpanded(true);

        final Composite composite = new Composite(sctnSection, SWT.NONE);
        formToolkit.adapt(composite);
        formToolkit.paintBordersFor(composite);
        sctnSection.setClient(composite);
        final TableColumnLayout tcl_composite = new TableColumnLayout();
        composite.setLayout(tcl_composite);

        final TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
        table = tableViewer.getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        formToolkit.paintBordersFor(table);

        final TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
        final TableColumn tblclmnColumn = tableViewerColumn.getColumn();
        tcl_composite.setColumnData(tblclmnColumn, new ColumnWeightData(150, 100));
        tblclmnColumn.setText("Column");
        createContents();
    }

    protected void createContents() {
        setText("SWT Application");
        setSize(450, 300);

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }
}

感谢您的帮助.

推荐答案

这是一个有关ColumnWeightData布局的错误,其解决方法如下所示:

It's a bug concerning layout of ColumnWeightData with a workaround shown here: bugs.eclipse.org/bugs/show_bug.cgi?id=215997#c4

技巧是将weigthHint=1设置为保存TableViewerCompositeGridData.

The trick is to set weigthHint=1 to the GridData of the Composite that holds the TableViewer.

这篇关于JFace ColumnWeigthData导致父级增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆