SWT - 使表格可滚动

时间:2017-07-26 13:55:29

标签: java eclipse-plugin swt jface

我已经尝试了以下代码,并且表格显示没有任何滚动条。请帮助使SWT表可以水平和垂直滚动。

    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中可滚动小部件相关的基本理解。在这方面请帮助。

1 个答案:

答案 0 :(得分:3)

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

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