如何在java SWT表中实现分页

时间:2016-03-14 06:12:24

标签: java swt

我正在尝试构建RCP应用程序。我有一个填充的表 千行来自数据库。我想禁用表格的垂直滚动条,让它一次显示20行,然后显示下一个按钮。按下它时会显示接下来的20行。

1 个答案:

答案 0 :(得分:0)

你可以试试PageBook api's http://www.programcreek.com/java-api-examples/index.php?api=org.eclipse.ui.part.PageBook http://www.javased.com/index.php?api=org.eclipse.ui.part.PageBook

或者您也可以编写自己的分页逻辑。类似下面,我使用了RCP Framework的默认视图模板。将View.java类的createPartControl方法置于

    public void createPartControl(Composite parent) {
    GridLayout layout = new GridLayout(1, false);
    parent.setLayout(layout);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);

    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
            | SWT.V_SCROLL);
    viewer.setContentProvider(new ViewContentProvider());
    viewer.setLabelProvider(new ViewLabelProvider());
    viewer.getTable().setLayoutData(data);
    // Provide the input to the ContentProvider

    String x[] = new String[40];

    int i = 0;
    for (String s : x) {

        x[i] = "String " + i;
        i++;
    }

    final Map<Integer, String[]> inputMap = new HashMap<Integer, String[]>();

    int pages = createPagination(inputMap, x);

    viewer.setInput(inputMap.get(1));

    Composite paginationButtons = new Composite(parent, SWT.NONE);
    GridLayout buttonLayout = new GridLayout();

    buttonLayout.numColumns = pages;
    buttonLayout.makeColumnsEqualWidth = true;
    paginationButtons.setLayout(buttonLayout);

    for (int j = 0; j < pages; j++) {
        Button pageButton = new Button(paginationButtons, SWT.BORDER);
        pageButton.setText(Integer.toString(j + 1));
        pageButton.addSelectionListener(new SelectionListener() {

            @Override
            public void widgetSelected(SelectionEvent arg0) {

                Button b = (Button) arg0.widget;
                int SelectedPage = Integer.parseInt(b.getText());
                viewer.setInput(inputMap.get(SelectedPage));
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub

            }
        });
    }
}
private int createPagination(Map<Integer, String[]> inputMap, String[] x) {

    String[] temp = x.clone();
    int beginCount = 0;
    int endCount = 19;
    int totalPages = temp.length / 20;
    if (temp.length < 20) {
        endCount = temp.length;
        totalPages = 1;
    }
    if ((temp.length % 20) > 0) {
        totalPages++;
    }
    int counter = 1;

    while (counter <= totalPages) {

        inputMap.put(new Integer(counter),
                Arrays.copyOfRange(x, beginCount, endCount + 1));

        beginCount = beginCount + 20;
        endCount = endCount + 20;

        if (beginCount > temp.length) {
            break;
        }
        if (endCount > temp.length) {
            endCount = temp.length - 1;
        }
        counter++;
    }

    return totalPages;

}

我更愿意去寻找PageBook,如果那不起作用,你可以使用上面类似的代码。基本上我在点击按钮(1,2,3 ...)上改变setInput(),重新加载带有新数据的表格。 希望这有帮助