Java 我可以给列表分页吗

Java 我可以给列表分页吗,java,jlist,paginate,Java,Jlist,Paginate,有没有办法在JList中对结果进行分页,我显示了100个结果,每次只想显示20个。。我想用表格来显示我的结果,但我显示的是推特,所以它是一个有图像和文本的JPanel。。etc所以它是一个JList看看这个显示分页的JList 下面是课堂: /** * A paginated list. Only displays a specific number of rows * and allows you to page backwards and forwards through the lis

有没有办法在JList中对结果进行分页,我显示了100个结果,每次只想显示20个。。我想用表格来显示我的结果,但我显示的是推特,所以它是一个有图像和文本的JPanel。。etc所以它是一个JList

看看这个显示分页的JList

下面是课堂:

/**
 * A paginated list. Only displays a specific number of rows
 * and allows you to page backwards and forwards through the list
 * with the help of a toolbar.
 */
public class PaginatedList extends JPanel {

    private final int pageSize;
    private final JList list;
    private final ListModel model;

    private final int lastPageNum;
    private int currPageNum;    
    private JLabel countLabel ;
    private JButton first, prev, next, last;

    /**
     * @param list the jlist
     * @param pageSize the number of rows visible in the jlist
     */
    public PaginatedList(JList list, int pageSize) {
        super();
        this.pageSize = pageSize;
        this.list = list;
        this.model = list.getModel();

        //work out how many pages there are
        this.lastPageNum = model.getSize() / pageSize + (model.getSize() % pageSize != 0 ? 1 : 0);
        this.currPageNum = 1;

        setLayout(new BorderLayout());
        countLabel = new JLabel() ;
        add(countLabel, BorderLayout.NORTH);
        add(list, BorderLayout.CENTER);
        add(createControls(), BorderLayout.SOUTH);
        updatePage();
    }

    private JPanel createControls() {
        first = new JButton(new AbstractAction("<<") {
            public void actionPerformed(ActionEvent e) {
                currPageNum = 1;
                updatePage();
            }
        });

        prev = new JButton(new AbstractAction("<") {
            public void actionPerformed(ActionEvent e) {
                if (--currPageNum <= 0)
                    currPageNum = 1;                
                updatePage();
            }
        });

        next = new JButton(new AbstractAction(">") {
            public void actionPerformed(ActionEvent e) {
                if (++currPageNum > lastPageNum)
                    currPageNum = lastPageNum;
                updatePage();

            }
        });

        last = new JButton(new AbstractAction(">>") {
            public void actionPerformed(ActionEvent e) {
                currPageNum = lastPageNum;
                updatePage();
            }
        });

        JPanel bar = new JPanel(new GridLayout(1, 4)); 
        bar.add(first);
        bar.add(prev);
        bar.add(next);
        bar.add(last);
        return bar;
    }

    private void updatePage() {

        //replace the list's model with a new model containing 
        //only the entries in the current page.
        final DefaultListModel page = new DefaultListModel();
        final int start = (currPageNum - 1) * pageSize;
        int end = start + pageSize;
        if (end >= model.getSize()) {
            end = model.getSize();
        }
        for (int i = start; i < end; i++) {
            page.addElement(model.getElementAt(i));
        }
        list.setModel(page);

        //update the label
        countLabel.setText("Page " + currPageNum + "/" + lastPageNum);

        // update buttons
        final boolean canGoBack = currPageNum != 1;
        final boolean canGoFwd = currPageNum != lastPageNum;
        first.setEnabled(canGoBack);
        prev.setEnabled(canGoBack);
        next.setEnabled(canGoFwd);
        last.setEnabled(canGoFwd);
    }    
}
/**
*有页码的清单。仅显示特定数量的行
*并允许您在列表中前后翻页
*在工具栏的帮助下。
*/
公共类分页列表扩展了JPanel{
私有最终整型页面大小;
私人最终名单;
私有最终列表模型;
私有final int lastPageNum;
私有int currPageNum;
私有JLabel计数标签;
私有按钮第一、上、下、最后;
/**
*@param列出jlist
*@param pageSize jlist中可见的行数
*/
公共分页列表(JList列表,int pageSize){
超级();
this.pageSize=页面大小;
this.list=列表;
this.model=list.getModel();
//算出有多少页
this.lastPageNum=model.getSize()/pageSize+(model.getSize()%pageSize!=0?1:0);
this.currPageNum=1;
setLayout(新的BorderLayout());
countLabel=新的JLabel();
添加(countLabel,BorderLayout.NORTH);
添加(列表、边框布局、中心);
添加(createControls(),BorderLayout.SOUTH);
updatePage();
}
私有JPanel createControls(){
first=新的JButton(新的AbstractAction(“>”){
已执行的公共无效操作(操作事件e){
currPageNum=lastPageNum;
updatePage();
}
});
JPanel条=新的JPanel(新的网格布局(1,4));
加上(第一);
加上(上一页);
添加(下一步);
添加(最后一个);
返回杆;
}
私有void updatePage(){
//将列表的模型替换为包含
//仅显示当前页面中的条目。
最终DefaultListModel页面=新建DefaultListModel();
最终整数开始=(currPageNum-1)*页面大小;
int end=start+pageSize;
如果(end>=model.getSize()){
end=model.getSize();
}
for(int i=start;i
用法示例:

public static void main(String args[]) throws Exception {

    // create 100 elements of dummy data. 
    Integer[] data = new Integer[100];
    for (int i = 0; i < data.length; i++) {
        data[i] = i + 1;
    }

    // create a paginated list with page size 20
    PaginatedList list = new PaginatedList(new JList(data), 20);

    // add it to a frame
    JFrame f = new JFrame();
    f.add(list);
    f.setSize(100, 100);
    f.pack();
    f.setVisible(true);
}
publicstaticvoidmain(字符串args[])引发异常{
//创建100个虚拟数据元素。
整数[]数据=新整数[100];
对于(int i=0;i
这会有所帮助