无法在滚动的组合中显示 TableViewer

Unable to display TableViewer inside a scrolled composite

我正在尝试显示 2 table 个查看器和他们之间的按钮。当试图 实现TableViewer显示水平和垂直滚动条,我看不到TableViewer,只能看到ScrolledComposite。我什至尝试在 ScrolledComposite 中创建一个合成并使用该合成来显示 TableViewer,但仍然看不到 TableViewer。有人能给我指出正确的方向吗?

@Override
protected Control createDialogArea(Composite parent) {
    GridData gridData = new GridData(SWT.NONE, SWT.FILL, true, true);
    gridData.heightHint = 500;
    gridData.widthHint = 700;
    Composite parentComposite = new Composite(parent, SWT.NONE);
    parentComposite.setLayout(new GridLayout(3, true));
    parentComposite.setLayoutData(gridData);
    ScrolledComposite scrolledComposite_1 = new ScrolledComposite(parentComposite, SWT.H_SCROLL|SWT.V_SCROLL);
    scrolledComposite_1.setLayout(new GridLayout(1,false));
    gridData = new GridData(SWT.NONE, SWT.FILL, true, true);
    gridData.heightHint = 200;
    gridData.widthHint = 400;
    scrolledComposite_1.setLayoutData(gridData);
    scrolledComposite_1.setAlwaysShowScrollBars(true);
    TableViewer tableViewer1 = new TableViewer(scrolledComposite_1, SWT.BORDER | SWT.SINGLE);
    tableViewer1.getTable().setHeaderVisible(true);
    tableViewer1.getTable().setLayout(new GridLayout(1,false));
    tableViewer1.getTable().setLayoutData(new GridData(200,400));
    createButtonComposite(parentComposite,SWT.NONE);
    ScrolledComposite scrolledComposite_2 = new ScrolledComposite(parentComposite, SWT.H_SCROLL|SWT.V_SCROLL);
    scrolledComposite_2.setLayout(new GridLayout(1,false));
    gridData = new GridData(SWT.NONE, SWT.FILL, true, true);
    gridData.heightHint = 200;
    gridData.widthHint = 400;
    scrolledComposite_2.setLayoutData(gridData);
    scrolledComposite_2.setAlwaysShowScrollBars(true);
    TableViewer tableViewer2= new TableViewer(scrolledComposite_2, SWT.BORDER | SWT.SINGLE);
    tableViewer2.getTable().setHeaderVisible(true);
    tableViewer2.getTable().setLayoutData(new GridData(200,400));
    getShell().setText("My Dialog"); 
    return parent;
}

ScrolledComposite 需要您调用 setContents 来告诉它子控件,还需要不同的其他设置。这有效:

scrolledComposite_1.setContent(tableViewer1.getTable());
scrolledComposite_1.setExpandVertical(true);
scrolledComposite_1.setExpandHorizontal(true);
scrolledComposite_1.setMinSize(tableViewer1.getTable().computeSize(SWT.DEFAULT, SWT.DEFAULT));

(第二个也一样)

但是 TableViewer 可以显示滚动条本身,所以你根本不需要 ScrolledComposite:

Composite parentComposite = new Composite(parent, SWT.NONE);
parentComposite.setLayout(new GridLayout(3, true));
parentComposite.setLayoutData(gridData);

TableViewer tableViewer1 = new TableViewer(parentComposite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL);
tableViewer1.getTable().setHeaderVisible(true);
tableViewer1.getTable().setLayout(new GridLayout(1,false));
tableViewer1.getTable().setLayoutData(new GridData(200,400));

createButtonComposite(parentComposite,SWT.NONE);

TableViewer tableViewer2= new TableViewer(parentComposite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL);
tableViewer2.getTable().setHeaderVisible(true);
tableViewer2.getTable().setLayoutData(new GridData(200,400));