How to Provide Pagination Support to a Jtable in Swing

How to load exact number of rows to jTable from MySQL and paginate the remaining?

An opinion-tip: Do not swallow exceptions, and do not face any specific type of exception as Exception. In your case, you should be catching them as SQLException and at least print their stacktrace.

Since your first page - query is "select * from students order by name ASC limit 100" the next page - next query should be something like "select * from students order by name ASC limit "+numClick+" , 100" with numClick increased by 100 in every page. Of course numClick variable should be handled by a PreparedStatement. Read here how to use prepared statements.

In order to understand LIMIT take a look at this answered question in addition.

Another way that you could give a chance, if your data is kind of small, is to load everything in memory (a data structure), and then populate the data from there.

When you change page, you will have to current data from the table. A simple search will guide you in this question.

java swing make Pagination Next/Prev , Next button Forword Record or Prev goback on button press

When you need to learn something new or fix something which is not working as expected it is often much easier to do it on a smaller scale, rather than on you application.

This goes in line with SO requirement of posting mre when asking or answering.

Your question is essentially how to change JPanels in their container, and the following code demonstrates just that, and nothing else:

import java.awt.*;
import javax.swing.*;

public class CardLayoutPrevNextDemo extends JPanel {

private final CardLayout cLayout;
private final JPanel cardsPane;
private static int MAX_PAGES = 5;
private int topPageIndex = 0;

public CardLayoutPrevNextDemo(){

cardsPane = new JPanel();
cLayout = new CardLayout();
cardsPane.setLayout(cLayout);
addPages();
changePane();

JButton prevPage = new JButton("Prev");
prevPage.addActionListener(e -> showPrevPane());
JButton nextPage = new JButton("Next");
nextPage.addActionListener(e -> showNextPane());

JPanel buttonsPane = new JPanel(new BorderLayout());
buttonsPane.add(prevPage,BorderLayout.WEST);
buttonsPane.add(nextPage,BorderLayout.EAST);

setLayout(new BorderLayout());
add(cardsPane,BorderLayout.CENTER);
add(buttonsPane,BorderLayout.NORTH);
}

private void addPages() {
for(int i = 0; i < MAX_PAGES ; i++){
JPanel pane = new JPanel();
String pageName = String.valueOf(i);
setPreferredSize(new Dimension(400,200));
pane.add(new JLabel("Page # "+pageName));
cardsPane.add(pageName, pane);
}
}

void showNextPane() {
topPageIndex = topPageIndex + 1 >= MAX_PAGES ? 0: topPageIndex + 1;
changePane();
}

void showPrevPane() {
topPageIndex = topPageIndex - 1 < 0 ? MAX_PAGES -1 : topPageIndex - 1;
changePane();
}

void changePane() {
cLayout.show(cardsPane, String.valueOf(topPageIndex));
}

public static void main(String[] args) {

JFrame frame = new JFrame("Card Layout Prev Next Demo");
frame.setSize(400,250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.add(new CardLayoutPrevNextDemo());
frame.pack();
frame.setVisible(true);
}
}

Sample Image

Can i paginate a JList

Take a look at this link which displays a paginated jlist.

Here is the class:

/**
* 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);
}
}

Example usage:

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);
}


Related Topics



Leave a reply



Submit