SWT - 使 table 可滚动

SWT - Making a table scrollable

我试过下面的代码, table 出现时没有任何滚动条。请帮助使 SWT table 水平和垂直滚动。

    package snippet;

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        createUI(shell);


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

    private static void createUI(Shell shell) {
        // TODO Auto-generated method stub
        GridLayout layout = new GridLayout(1, false);


        shell.setLayout(layout);


        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION|SWT.CHECK|SWT.RESIZE|SWT.SCROLL_PAGE);
        GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true);
        gd_table.heightHint = 136;
        gd_table.widthHint = 205;
        table.setLayoutData(gd_table);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
        tblclmnNewColumn.setWidth(37);        

        TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
        tblclmnNewColumn_1.setWidth(219);
        tblclmnNewColumn_1.setText("Item ID");

        TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
        tblclmnNewColumn_2.setWidth(246);
        tblclmnNewColumn_2.setText("Revision ID");

        TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
        tblclmnNewColumn_3.setWidth(246);
        tblclmnNewColumn_3.setText("Vendor Name");

        TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
        tblclmnNewColumn_4.setWidth(246);
        tblclmnNewColumn_4.setText("Vendor ID");

        for(int i=0;i<10;i++)
        {
            TableItem item = new TableItem(table, SWT.NONE);

            item.setText(1,"item_id"+i);
            item.setText(2,"item_revision_id"+i);
            item.setText(3,"object_string"+i);
        }

    }

}

我尝试同时使用 SWT.SCROLL_PAGE 和 SWT.SCROLL_LINE。在此示例中,两者都给出了相同的结果。我缺少与 SWT 中的可滚动小部件相关的基本理解。请在这方面提供帮助。

您要查找的值是 SWT.V_SCROLLSWT.H_SCROLL。如果用这两个替换 Table 构造函数中的 SWT.SCROLL_PAGE 常量,您的代码将按预期工作。

Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK | SWT.RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);