java - JFace ColumnWeigthData 导致父级增长

标签 java swt eclipse-rcp jface tableviewer

我有一个 Eclipse RCP 应用程序,想在 TableViewer 中使用动态列大小,使用 ColumnWeigthData 作为 ColumnLayoutData。问题是每当我布置表格时,父窗体(示例代码中的 ScrolledForm)都会增长几个像素。

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

为什么它会这样做,我怎样才能让它停止?

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 布局的错误,解决方法如下:bugs.eclipse.org/bugs/show_bug.cgi?id=215997#c4

技巧是将 weigthHint=1 设置为包含 TableViewerCompositeGridData

关于java - JFace ColumnWeigthData 导致父级增长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15971480/

相关文章:

java - 测试 Camel quartz 路线

java - Java 中的列表、链表、数组列表

java - 调整表格列的大小以填充所有可用空间

java - 如何在 Eclipse RCP 应用程序中使 View 可滚动?

java - 读取文件并放入一些数组中

java - jmx/jstatd 通过 ssh 隧道访问远程机器

text - SWT 文本框边框颜色

java - 关于样式为 SWT.CALENDAR 的 DateTime 小部件

java - 在 eclipse RCP 中显示 View 时如何显示上下文帮助

Eclipse RCP & tycho - 无法解析类型 org.eclipse.swt.widgets.Button。它是从所需的 .class 文件中间接引用的