title: How to provide paging to a JTable in Swing instead of scroll bar?
body: <p>I have been developing an application that requires me to implement pagination in JTable. I populate the JTable with the data in Microsoft Access database. All this works perfectly.I really don't know how to implement the concept of pagination in my JTable. I have over 15000 records in my database, all 15000 records are showing in one page with scroll bar so it is getting slow, I want to show 50 records per page. So, when the user clicks on the next button, the next 50 records are displayed and so on..
  I have created GUI in Swing Java in which I have used JTable. How should I do that ?
  it is my jtable class where i get data and display into jtable.</p>
  
  <pre><code>package com.ktech.voters;
  
  import java.awt.Color;
  import java.awt.event.KeyEvent;
  import java.text.ParseException;
  import java.util.Vector;
  import javax.swing.JFormattedTextField;
  import javax.swing.JOptionPane;
  import javax.swing.JTextField;
  import javax.swing.RowFilter;
  import javax.swing.table.DefaultTableModel;
  import javax.swing.table.TableRowSorter;
  import javax.swing.text.MaskFormatter;
  
  /**
   *
   * @author ahmad
   */
  public class votersTable extends javax.swing.JFrame {
  
      DefaultTableModel dm;
      /**
       * Creates new form votersTable
       */
      private Vector&lt;Vector&lt;String&gt;&gt; data;
      private Vector&lt;String&gt; header; 
  
      public votersTable() throws Exception {
          votersDAO dao = new votersDAO();
          data = dao.getVoters();
  
          header = new Vector&lt;String&gt;();
          header.add("ID");
          header.add("Naam");
          header.add("walid ka naam");
          header.add("CNIC number");
          header.add("Polling Station Number");
          header.add("booth number");
  
          initComponents();
      }
  
      private void filter(String query)
      {
          dm = (DefaultTableModel) jTable1.getModel();
          TableRowSorter&lt;DefaultTableModel&gt; tr = new TableRowSorter&lt;DefaultTableModel&gt;(dm);
          jTable1.setRowSorter(tr);
  
          tr.setRowFilter(RowFilter.regexFilter(query));
      }
  
  //    private void validator(String q)
  //    {
  //        if (q.matches(".*\\d.*"))
  //        {
  //            idSearch.setEditable(true);
  //            idSearch.setBackground(Color.blue);
  //        }
  //        else 
  //        {
  //            idSearch.setEditable(false);
  //            idSearch.setBackground(Color.red);
  //        }
  //
  //        
  //    }
  
      /**
       * This method is called from within the constructor to initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is always
       * regenerated by the Form Editor.
       */
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
  
          jScrollPane1 = new javax.swing.JScrollPane();
          jTable1 = new javax.swing.JTable();
          jLabel1 = new javax.swing.JLabel();
          idSearch = new javax.swing.JTextField();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          jTable1.setModel(new javax.swing.table.DefaultTableModel(
              data,header
          ));
          jScrollPane1.setViewportView(jTable1);
  
          jLabel1.setText("Search by CNIC");
  
          idSearch.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  idSearchActionPerformed(evt);
              }
          });
          idSearch.addKeyListener(new java.awt.event.KeyAdapter() {
              public void keyPressed(java.awt.event.KeyEvent evt) {
                  idSearchKeyPressed(evt);
              }
              public void keyReleased(java.awt.event.KeyEvent evt) {
                  idSearchKeyReleased(evt);
              }
              public void keyTyped(java.awt.event.KeyEvent evt) {
                  idSearchKeyTyped(evt);
              }
          });
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(layout.createSequentialGroup()
                          .addGap(90, 90, 90)
                          .addComponent(jLabel1)
                          .addGap(37, 37, 37)
                          .addComponent(idSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGroup(layout.createSequentialGroup()
                          .addGap(32, 32, 32)
                          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 939, javax.swing.GroupLayout.PREFERRED_SIZE)))
                  .addContainerGap(31, Short.MAX_VALUE))
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addGap(55, 55, 55)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                      .addComponent(jLabel1)
                      .addComponent(idSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(77, 77, 77))
          );
  
          pack();
      }// &lt;/editor-fold&gt;                        
  
      private void idSearchKeyReleased(java.awt.event.KeyEvent evt) {                                     
  
          String query=idSearch.getText();
          filter(query);
      }                                    
  
      private void idSearchKeyPressed(java.awt.event.KeyEvent evt) {                                    
  
  //        String q=idSearch.getText();
  //        validator(q);
          int key = evt.getKeyCode();
          if((key&gt;=evt.VK_0&amp;&amp;key&lt;=evt.VK_9)||(key&gt;=evt.VK_NUMPAD0&amp;&amp;key&lt;=evt.VK_NUMPAD9)
            ||key==KeyEvent.VK_BACK_SPACE||key==KeyEvent.VK_MINUS)
          {
              idSearch.setEditable(true);
          }
          else
          {
              idSearch.setEditable(false);
              idSearch.setBackground(Color.red);
          }
  //        
  //        
  //        
  //        if (check.matches(".*\\d.*"))
  //        {
  //            idSearch.setEditable(true);
  //            idSearch.setBackground(Color.YELLOW);
  //        }
  //        else
  //        {
  //            idSearch.setEditable(false);
  //            idSearch.setBackground(Color.red);
  //        }
  
      }                                   
  
      private void idSearchKeyTyped(java.awt.event.KeyEvent evt) {                                  
  
  //        String q=idSearch.getText();
  //        validator(q);
  
      }                                 
  
      private void idSearchActionPerformed(java.awt.event.ActionEvent evt) {                                         
          // TODO add your handling code here:
      }                                        
  
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
          /* Set the Nimbus look and feel */
          //&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;
          /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
           * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
           */
          try {
              for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                  if ("Nimbus".equals(info.getName())) {
                      javax.swing.UIManager.setLookAndFeel(info.getClassName());
                      break;
                  }
              }
          } catch (ClassNotFoundException ex) {
              java.util.logging.Logger.getLogger(votersTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(votersTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
              java.util.logging.Logger.getLogger(votersTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(votersTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
          //&lt;/editor-fold&gt;
  
          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try
                  {
                  new votersTable().setVisible(true);
                  }
                  catch (Exception e)
                  {
                      System.out.println("cannot run");
                  }
              }
          });
      }
  
      // Variables declaration - do not modify                     
      private javax.swing.JTextField idSearch;
      private javax.swing.JLabel jLabel1;
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JTable jTable1;
      // End of variables declaration                   
  }
  </code></pre>
  
  <p>it is my dao class in which i retrieve data and display in jtable in jtable class mentioned above.</p>
  
  <pre><code>import java.sql.Statement;
  import java.util.Vector;
  
  
  /**
   *
   * @author ahmad
   */
  public class votersDAO extends dbUtility {
  
      private static Statement st;
      private static ResultSet rs;
          public Vector getVoters()throws Exception
          {
              Vector&lt;Vector&lt;String&gt;&gt; votersVector = new Vector&lt;Vector&lt;String&gt;&gt;();
  
              Connection connection = getConnection();
              st = connection.createStatement();
              rs = st.executeQuery("select * from Voters");
  
          while(rs.next())
          {
              Vector&lt;String&gt; voters = new Vector&lt;String&gt;();
              voters.add(rs.getString(1)); 
              voters.add(rs.getString(11));
              voters.add(rs.getString(12));
              voters.add(rs.getString(13));
              voters.add(rs.getString(17));
              voters.add(rs.getString(20));
              votersVector.add(voters);
          }
  
      if(connection!=null)
      connection.close();
  
  return votersVector;
  }
  </code></pre>
  
  <p>please kindly request you guys to help me out here. Any example that can be merged with my code would really be helpful.</p>