When I first used jframe to program, I wrote it simpler. I just first query all records, and then filtered out the records of the current page according to the current page number. The returned small set can be displayed directly on the table.
import java.util.ArrayList; import java.util.List; import com.yu.entity.User; public class PageController { private List<User> bigList=new ArrayList<User>(); //Large collection, get private List<User> smallList=new ArrayList<User>(); //Small collection, return to the class that calls it private static int currentPageIndex=1; //Current page number private int countPerpage=10; //Number of displays for each page private int pageCount; //Total number of pages private int recordCount; //Total number of records//Initialize block, when calling the class, it will automatically load { //Calendar the method of querying the database and return a List this.bigList = new Utils().query(); //Calculate the total number of pages if (bigList.size()%countPerpage==0) { this.pageCount=bigList.size()/countPerpage; } else { this.pageCount=(bigList.size()/countPerpage)+1; } } public PageController() { } //Pass in the constructor of the specified page number, see which page. public PageController(int currentPageIndex){ this.currentPageIndex=currentPageIndex; } //Get exact record of the current page and return a list public List<User> setCurentPageIndex() { return select(); } //Next page public List<User> nextPage() { if (currentPageIndex < pageCount ) { currentPageIndex++; System.out.println("Current page:"+currentPageIndex); } return select(); } //Previous page public List<User> previousPage() { if (currentPageIndex > 1) { currentPageIndex--; System.out.println("Current page:"+currentPageIndex); } return select(); } //This method is for the above method call, filter records based on the current page public List<User> select(){ recordCount=bigList.size(); for(int i=(currentPageIndex-1)*countPerpage; i<currentPageIndex*countPerpage&&i<recordCount; i++){ smallList.add(bigList.get(i)); } return smallList; } }The following is the content written in the jframe, calling the pagination method.
//Query the previous page private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) { List<User> newlist = new PageController().previousPage(); viewAll(newlist); } //Return the record of the first page private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) { List<User> newlist = new PageController(1).setCurentPageIndex(); viewAll(newlist); } //Query the next page private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) { List<User> newlist = new PageController().nextPage(); viewAll(newlist); } //Show users in List public void viewAll(List<User> list) { Object[][] objects = new Object[(list.size())][]; for (int i = 0; i < objects.length; i++) { objects[i] = new Object[3]; objects[i][0] = i + 1; objects[i][1] = list.get(i).getUserid(); objects[i][2] = list.get(i).getTruename(); } jTable1.setModel(new javax.swing.table.DefaultTableModel(objects, new String[] { "Line Number", "Work Number", "Name" })); }The above is the implementation method of paginating the data into a table that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!