How to provide pagination support to a JTable in Swing?

I have created one GUI in Swing Java in which I have used JTable.Now I want to display next page information into it by using pagination. How should I do that ?


Paging in a Swing JTable looks like a nice article.

Here is an excerpt:

As far as I remember the solution for this problem lies in the concept of paging: just retrieve the data that the user wants to see and nothing more. This also means you have to sometimes get extra data from the db server (or appserver) if your user scrolls down the list.

Big was my surprise that there wasn't really an out-of-the-box solution (not even a copy- paste solution) for this problem. Anyone that knows one, please don't hesitate to extend my (rather limited) knowledge of the J2EE platform.

So we dug in, and tried to build a solution ourselves. What we eventually came up with was an adapted TableModel class to takes care of the paging.


Another option to implement this is to use a scrollbar-less scrollpane and a couple nav buttons to effect the control. The buttons that have been added are normal JButtons for the prototype.

A quick prototype is added below. It makes a couple assumptions, one of which is that the table model has all of its data. Work could be done to ensure that rows end up flush at the top of the view upon navigation.

private void buildFrame() {
    frame = new JFrame("Demo");
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addStuffToFrame();
    frame.setVisible(true);

}

private void addStuffToFrame() {
    final JTable table = getTable();
    final JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    final JButton next = new JButton("next");
    final JButton prev = new JButton("prev");

    ActionListener al = new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            Rectangle rect = scrollPane.getVisibleRect();
            JScrollBar  bar = scrollPane.getVerticalScrollBar();
            int blockIncr = scrollPane.getViewport().getViewRect().height;
            if (e.getSource() == next) {
                bar.setValue(bar.getValue() + blockIncr);
            } else if (e.getSource() == prev) {
                bar.setValue(bar.getValue() - blockIncr);
            }
            scrollPane.scrollRectToVisible(rect);
        }
    };

    next.addActionListener(al);
    prev.addActionListener(al);

    JPanel panel = new JPanel(new BorderLayout());
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(prev);
    buttonPanel.add(next);
    panel.add(buttonPanel, BorderLayout.NORTH);
    panel.add(scrollPane, BorderLayout.CENTER);
    frame.getContentPane().add(panel);
}

private JTable getTable() {
    String[] colNames = new String[]{
            "col 0", "col 1", "col 2", "col 3"
    };

    String[][] data = new String[100][4];
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 4; j++) {
            data[i][j] = "r:" + i + " c:" + j;
        }
    }

    return new JTable(data,colNames);

}

alt text http://img7.imageshack.us/img7/4205/picture4qv.png