aquaballoon

Java - Pagination

Apr 27th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.10 KB | None | 0 0
  1. package mysql;
  2.  
  3. import java.awt.*;
  4. import java.sql.*;
  5. import java.util.*;
  6. import javax.swing.*;
  7. import java.awt.event.*;
  8. import javax.swing.text.*;
  9. import javax.swing.table.*;
  10. import javax.swing.plaf.basic.*;
  11.  
  12. public class Pagination extends JPanel {
  13.  
  14.     RadioButtonUI ui = new RadioButtonUI();
  15.     int pageSize = 5;
  16.  
  17.     Model model = new Model();
  18.     TableRowSorter sorter = new TableRowSorter(model);
  19.     Box box = Box.createHorizontalBox();
  20.  
  21.     public Pagination() {
  22.         super(new BorderLayout());
  23.         JTable table = new JTable(model) {
  24.             @Override
  25.             public Component prepareRenderer(TableCellRenderer tcr, int row,
  26.                     int column) {
  27.                 Component c = super.prepareRenderer(tcr, row, column);
  28.                 if (isRowSelected(row)) {
  29.                     c.setForeground(getSelectionForeground());
  30.                     c.setBackground(getSelectionBackground());
  31.                 } else {
  32.                     c.setForeground(getForeground());
  33.                     c.setBackground((row % 2 == 0) ? Color.lightGray
  34.                             : getBackground());
  35.                 }
  36.                 return c;
  37.             }
  38.         };
  39.         table.setIntercellSpacing(new Dimension());
  40.         table.setShowGrid(false);
  41.         table.setRowSorter(sorter);
  42.         showPages(10, 1);
  43.  
  44.         add(new JScrollPane(table));
  45.         add(box, BorderLayout.SOUTH);
  46.         setPreferredSize(new Dimension(320, 240));
  47.     }
  48.  
  49.     @SuppressWarnings("empty-statement")
  50.     private void showPages(final int itemsPerPage, final int currentPageIndex) {
  51.         sorter.setRowFilter(filter(itemsPerPage, currentPageIndex - 1));
  52.         ArrayList<JRadioButton> pageList = new ArrayList<JRadioButton>();
  53.  
  54.         int startPageIndex = currentPageIndex - pageSize;
  55.         if (startPageIndex <= 0) {
  56.             startPageIndex = 1;
  57.         }
  58.         int maxPageIndex = (model.getRowCount() / itemsPerPage) + 1;
  59.         int endPageIndex = currentPageIndex + pageSize - 1;
  60.         if (endPageIndex > maxPageIndex) {
  61.             endPageIndex = maxPageIndex;
  62.         }
  63.  
  64.         if (currentPageIndex > 1) {
  65.             pageList.add(createRadioButtons(itemsPerPage, currentPageIndex - 1, "Prev"));
  66.         }
  67.         for (int i = startPageIndex; i <= endPageIndex; i++) {
  68.             pageList.add(createLinks(itemsPerPage, currentPageIndex, i - 1));
  69.         }
  70.         if (currentPageIndex < maxPageIndex) {
  71.             pageList.add(createRadioButtons(itemsPerPage, currentPageIndex + 1, "Next"));
  72.         }
  73.  
  74.         box.removeAll();
  75.         ButtonGroup bg = new ButtonGroup();
  76.         box.add(Box.createHorizontalGlue());
  77. //                for (int x=0; x < list.size(); x++) {
  78. //                        JRadioButton radio = new JRadioButton("HI");
  79. //                        box.add(radio);
  80. //                        bg.add(radio);
  81. //                }
  82.  
  83.         for (JRadioButton r : pageList) {
  84.             box.add(r);
  85.             bg.add(r);
  86.             System.out.println(r);
  87.         }
  88.  
  89.         box.add(Box.createHorizontalGlue());
  90.         box.revalidate();
  91.         box.repaint();
  92.         pageList.clear();
  93.     }
  94.  
  95.     private JRadioButton createLinks(final int itemsPerPage, final int current,
  96.             final int target) {
  97.         JRadioButton radio = new JRadioButton("" + (target + 1)) {
  98.             protected void fireStateChanged() {
  99.                 ButtonModel model = getModel();
  100.                 if (!model.isEnabled()) {
  101.                     setForeground(Color.GRAY);
  102.                 } else if (model.isPressed() && model.isArmed()) {
  103.                     setForeground(Color.GREEN);
  104.                 } else if (model.isSelected()) {
  105.                     setForeground(Color.RED);
  106.                 }
  107.                 super.fireStateChanged();
  108.             }
  109.         };
  110.         radio.setForeground(Color.BLUE);
  111.         radio.setUI(ui);
  112.         if (target + 1 == current) {
  113.             radio.setSelected(true);
  114.         }
  115.         radio.addActionListener(new ActionListener() {
  116.             public void actionPerformed(ActionEvent e) {
  117.                 showPages(itemsPerPage, target + 1);
  118.             }
  119.         });
  120.         return radio;
  121.     }
  122.  
  123.     private JRadioButton createRadioButtons(final int itemsPerPage,
  124.             final int target, String title) {
  125.         JRadioButton radio = new JRadioButton(title);
  126.         radio.setForeground(Color.BLUE);
  127.         radio.setUI(ui);
  128.         radio.addActionListener(new ActionListener() {
  129.             @Override
  130.             public void actionPerformed(ActionEvent e) {
  131.                 showPages(itemsPerPage, target);
  132.             }
  133.         });
  134.         return radio;
  135.     }
  136.  
  137.     private RowFilter filter(final int itemsPerPage,
  138.             final int target) {
  139.         return new RowFilter() {
  140.             public boolean include(
  141.                     Entry entry) {
  142.                 //int ei = entry.getIdentifier();
  143.                 int ei = (int) entry.getIdentifier();
  144.                 return (target * itemsPerPage <= ei && ei < target
  145.                         * itemsPerPage + itemsPerPage);
  146.             }
  147.         };
  148.     }
  149.  
  150.     public static void main(String[] args) {
  151.         JFrame frame = new JFrame("Table");
  152.         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  153.         frame.getContentPane().add(new Pagination());
  154.         frame.pack();
  155.         frame.setLocationRelativeTo(null);
  156.         frame.setVisible(true);
  157.     }
  158. }
  159.  
  160. class RadioButtonUI extends BasicRadioButtonUI {
  161.  
  162.     public Icon getDefaultIcon() {
  163.         return null;
  164.     }
  165.  
  166.     private static Dimension size = new Dimension();
  167.     private static Rectangle rec1 = new Rectangle();
  168.     private static Rectangle rec2 = new Rectangle();
  169.     private static Rectangle rec3 = new Rectangle();
  170.  
  171.     public synchronized void paint(Graphics g, JComponent c) {
  172.         AbstractButton b = (AbstractButton) c;
  173.         ButtonModel model = b.getModel();
  174.         Font f = c.getFont();
  175.         g.setFont(f);
  176.         FontMetrics fm = c.getFontMetrics(f);
  177.  
  178.         Insets i = c.getInsets();
  179.         size = b.getSize(size);
  180.         rec1.x = i.left;
  181.         rec1.y = i.top;
  182.         rec1.width = size.width - (i.right + rec1.x);
  183.         rec1.height = size.height - (i.bottom + rec1.y);
  184.         rec2.x = rec2.y = rec2.width = rec2.height = 0;
  185.         rec3.x = rec3.y = rec3.width = rec3.height = 0;
  186.  
  187.         String text = SwingUtilities.layoutCompoundLabel(c, fm, b.getText(),
  188.                 null, b.getVerticalAlignment(), b.getHorizontalAlignment(), b
  189.                 .getVerticalTextPosition(), b
  190.                 .getHorizontalTextPosition(), rec1, rec2, rec3, 0);
  191.  
  192.         if (c.isOpaque()) {
  193.             g.setColor(b.getBackground());
  194.             g.fillRect(0, 0, size.width, size.height);
  195.         }
  196.         if (text == null) {
  197.             return;
  198.         }
  199.         g.setColor(b.getForeground());
  200.         if (!model.isSelected() && !model.isPressed() && !model.isArmed()
  201.                 && b.isRolloverEnabled() && model.isRollover()) {
  202.             g.drawLine(rec1.x, rec1.y + rec1.height, rec1.x + rec1.width,
  203.                     rec1.y + rec1.height);
  204.         }
  205.         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  206.         if (v != null) {
  207.             v.paint(g, rec3);
  208.         } else {
  209.             paintText(g, b, rec3, text);
  210.         }
  211.     }
  212. }
  213.  
  214. class Model extends DefaultTableModel {
  215.  
  216.     Model() {
  217.         JTable table = new JTable(this);
  218.         addColumn("Name");
  219.         addColumn("Email");
  220.         try {
  221.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  222.             Connection con = DriverManager.getConnection(
  223.                     "jdbc:mysql://localhost:3306/ITDB", "root", "abc123");
  224.             String query = "select * from ITTB";
  225.             Statement st = con.createStatement();
  226.             ResultSet rs = st.executeQuery(query);
  227.  
  228.             while (rs.next()) {
  229.                 String name = rs.getString("name");
  230.                 String email = rs.getString("email");
  231.                 addRow(new Object[]{name, email});
  232.             }
  233.         } catch (Exception e) {
  234.         }
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment