Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaSWT表中实现分页_Java_Swt - Fatal编程技术网

如何在JavaSWT表中实现分页

如何在JavaSWT表中实现分页,java,swt,Java,Swt,我正在尝试构建一个RCP应用程序。我有一个表,其中填充了 从数据库中删除1000行。我想禁用表格的垂直滚动条,并使其一次显示二十行和一个“下一步”按钮。按下该按钮后,将显示接下来的二十行。您可以尝试使用PageBook api 或者,您也可以编写自己的分页逻辑 public void createPartControl(Composite parent) { GridLayout layout = new GridLayout(1, false); parent.set

我正在尝试构建一个RCP应用程序。我有一个表,其中填充了 从数据库中删除1000行。我想禁用表格的垂直滚动条,并使其一次显示二十行和一个“下一步”按钮。按下该按钮后,将显示接下来的二十行。

您可以尝试使用PageBook api

或者,您也可以编写自己的分页逻辑

    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;

}
public void createPartControl(复合父级){
GridLayout=新的GridLayout(1,false);
父.setLayout(布局);
GridData数据=新的GridData(SWT.FILL,SWT.FILL,true,true);
查看器=新的TableViewer(父级,SWT.MULTI | SWT.H_滚动
|SWT.V_卷轴);
setContentProvider(新的ViewContentProvider());
setLabelProvider(新的ViewLabelProvider());
viewer.getTable().setLayoutData(数据);
//向ContentProvider提供输入
字符串x[]=新字符串[40];
int i=0;
for(字符串s:x){
x[i]=“字符串”+i;
i++;
}
final Map inputMap=new HashMap();
int pages=createPagination(inputMap,x);
setInput(inputMap.get(1));
复合分页按钮=新复合(父项,SWT.NONE);
GridLayout按钮布局=新建GridLayout();
buttonLayout.numColumns=页面;
buttonLayout.makeColumnsEqualWidth=true;
分页按钮。设置布局(按钮布局);
对于(int j=0;j0){
totalPages++;
}
int计数器=1;
while(计数器温度长度){
打破
}
如果(结束计数>温度长度){
endCount=温度长度-1;
}
计数器++;
}
返回总页数;
}
我更愿意选择PageBook,如果这不起作用,您可以使用上面类似的代码。基本上,我是在点击按钮(1,2,3…)后更改setInput(),这将用新数据重新加载表。 希望这有帮助