填充 TableViewer 时文本字段丢失

Text field gone missing when TableViewer gets filled

问题发生在 Win10 上。日蚀 4.13.0.

我在 GridLayout 中的文本小部件上方有一个 JFace Table查看器。每当我用内容填充 Table 时,文本小部件就会消失。如果我将 shell 配置为 FillLayout 它可以工作,但这不是我想要的,因为我有一些小部件不想获取任何 space(如搜索字段、分隔符等)。

我好像找不到问题,有什么建议吗?

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
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.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

public class TestDialog {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setBounds(10, 10, 800, 600);
        shell.setLayout(new GridLayout());

        Label separator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
        separator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        Link link = new Link(shell, SWT.NONE);
        link.setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore");
        link.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

        Text txtSearch = new Text(shell, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.CANCEL);
        txtSearch.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        txtSearch.setMessage("Enter search phrase here");

        TableViewer tableViewer = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
        tableViewer.setContentProvider(ArrayContentProvider.getInstance());

        Table table = tableViewer.getTable();
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

        Group grp = new Group(shell, SWT.NONE);
        grp.setText("MyGroup:");
        grp.setLayout(new FillLayout(SWT.HORIZONTAL));
        grp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

        Text txt = new Text(grp, SWT.WRAP);

        List<String> entries = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            entries.add("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore");
        }

        tableViewer.setInput(entries);      

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

正在扩展 table 以显示所有行,这会将 table 以下的所有内容从 window.

中推出

您需要为 table 指定高度提示:

Table table = tableViewer.getTable();
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
data.heightHint = 200;
table.setLayoutData(data);