【问题标题】:JFace ColumnWeigthData causes parent to growJFace ColumnWeigthData 导致父级增长
【发布时间】:2013-04-12 12:30:35
【问题描述】:

我有一个 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
    }
}

感谢您的帮助。

【问题讨论】:

  • 这是一个错误,此处显示了解决方法:bugs.eclipse.org/bugs/show_bug.cgi?id=215997#c4 技巧是将weigthHint=1 添加到包含CompositeGridData 中,其中包含TableViewer
  • 然后继续发布这个作为答案。
  • 没有足够的代表。得等6个小时。如果你愿意,你可以继续。感谢您对原始问题的修改,我会投票支持您。
  • 不,等 6 个小时。一旦答案在这里,我会投票,这样你就可以获得一些声誉......

标签: java swt eclipse-rcp jface tableviewer


【解决方案1】:

这是一个与 ColumnWeightData 布局有关的错误,此处显示了解决方法:bugs.eclipse.org/bugs/show_bug.cgi?id=215997#c4

诀窍是将weigthHint=1 设置为拥有TableViewerCompositeGridData

【讨论】:

  • 谢谢,这个提示对我帮助很大。可悲的是,尽管自 2008 年以来他们就无法解决这个问题......
【解决方案2】:

如果您使用上下布局(父尺寸定义明确),那么您可以覆盖 TableColumnLayout 以始终​​尊重父尺寸:

/** Forces table never to take more space than parent has*/
public class TableColumnLayout extends org.eclipse.jface.layout.TableColumnLayout {

    @Override
    protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
        return new Point(wHint, hHint);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2016-08-26
    • 1970-01-01
    • 2012-06-25
    相关资源
    最近更新 更多