浏览 2974 次
锁定老帖子 主题:分页封装2
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-04-29
1 新建接口 import java.util.List; public interface PageInterface { /** * 根据页数,每页显示行数返回jtable显示信息 * @param page * @param rows * @return */ public List getTableList(int page, int rows); } 2 根据路径查找类 public class PageFactory { /** * 根据路径查找类 * @param className 路径 * @return * @throws InstantiationException * @throws IllegalAccessException */ public static PageInterface getClass(String className) throws InstantiationException, IllegalAccessException { PageInterface production = null; try { production = (PageInterface) Class.forName (className).newInstance(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } return production; } } 3 根据路径,页数,每页显示行数得到jtable显示的信息 public class PageList { public List showListOfTable(String a, int page, int rows) throws InstantiationException, IllegalAccessException { PageInterface cat = PageFactory.getClass(a); List list = cat.getTableList(page, rows); return list; } } 4 建立一个model public class PageButtonTableModel extends DefaultTableModel { /** * */ private static final long serialVersionUID = 1L; String header[]; List<Object[]> data = new ArrayList<Object[]>(); public PageButtonTableModel() { } public List<Object[]> getData() { return data; } public void setData(List<Object[]> data) { this.data = data; } public int getColumnCount() { return header.length; } public String getColumnName(int c) { return header[c]; } public Object getValueAt(int row, int column) { return data.get(row)[column]; } public boolean isCellEditable(int r, int c) { return false; } public int getRowCount() { if (data != null) { return data.size(); } else { return 0; } } } 5 建立共用模板 public class ButtonPanel extends JPanel { private static final long serialVersionUID = 1L; private JButton firstButton = null; private JButton preButton = null; private JButton nextButton = null; private JButton lastButton = null; private JLabel jLabel = null; // ==================== private String page; private List<Object[]> tableData; private PageButtonTableModel model; private int totalPage; private int numberOfPage = 1; private String className; private PageList pList = new PageList(); // @jve:decl-index=0: private int rows; private JTable jTable; /** * * @param totalPage 总页数 * @param className 类名 * @param rows 每页显示行数 * @param jTable */ public ButtonPanel(int totalPage, String className, int rows, JTable jTable) { super(); this.totalPage = totalPage; this.className = className; this.rows = rows; this.jTable = jTable; page = "共 " + totalPage + " 页 当前第 " + numberOfPage + " 页"; initialize(); } /** * This method initializes this * * @return void */ private void initialize() { jLabel = new JLabel(); jLabel.setBounds(new Rectangle(270, 1, 129, 24)); jLabel.setText(page); this.setSize(400, 26); this.setLayout(null); this.add(getFirstButton(), null); this.add(getPreButton(), null); this.add(getNextButton(), null); this.add(getLastButton(), null); this.add(jLabel, null); } /** * This method initializes firstButton * * @return javax.swing.JButton */ private JButton getFirstButton() { if (firstButton == null) { firstButton = new JButton(); firstButton.setText("首页"); firstButton.setBounds(new Rectangle(0, 1, 60, 24)); firstButton.addActionListener(new java.awt.event.ActionListener() { @SuppressWarnings("unchecked") public void actionPerformed(java.awt.event.ActionEvent e) { numberOfPage = 1; try { tableData = pList.showListOfTable(className, numberOfPage, rows); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } setDataOfTable(jTable); } }); } return firstButton; } /** * This method initializes preButton * * @return javax.swing.JButton */ private JButton getPreButton() { if (preButton == null) { preButton = new JButton(); preButton.setText("上一页"); preButton.setBounds(new Rectangle(60, 1, 75, 24)); preButton.addActionListener(new java.awt.event.ActionListener() { @SuppressWarnings("unchecked") public void actionPerformed(java.awt.event.ActionEvent e) { if (numberOfPage <= totalPage && numberOfPage > 1) { numberOfPage = numberOfPage - 1; } try { tableData = pList.showListOfTable(className, numberOfPage, rows); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } setDataOfTable(jTable); } }); } return preButton; } /** * This method initializes nextButton * * @return javax.swing.JButton */ private JButton getNextButton() { if (nextButton == null) { nextButton = new JButton(); nextButton.setText("下一页"); nextButton.setBounds(new Rectangle(135, 1, 75, 24)); nextButton.addActionListener(new java.awt.event.ActionListener() { @SuppressWarnings("unchecked") public void actionPerformed(java.awt.event.ActionEvent e) { if (numberOfPage < totalPage) { numberOfPage = numberOfPage + 1; } try { tableData = pList.showListOfTable(className, numberOfPage, rows); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } setDataOfTable(jTable); } }); } return nextButton; } /** * This method initializes lastButton * * @return javax.swing.JButton */ private JButton getLastButton() { if (lastButton == null) { lastButton = new JButton(); lastButton.setText("尾页"); lastButton.setBounds(new Rectangle(210, 1, 60, 24)); lastButton.addActionListener(new java.awt.event.ActionListener() { @SuppressWarnings("unchecked") public void actionPerformed(java.awt.event.ActionEvent e) { numberOfPage = totalPage; try { tableData = pList.showListOfTable(className, numberOfPage, rows); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } setDataOfTable(jTable); } }); } return lastButton; } public void setDataOfTable(JTable jTable) { getModel().setData(tableData); jTable.updateUI(); page = "共 " + totalPage + " 页 当前第 " + numberOfPage + " 页"; jLabel.setText(page); if (numberOfPage == 1) { firstButton.setEnabled(false); preButton.setEnabled(false); nextButton.setEnabled(true); lastButton.setEnabled(true); } else if (numberOfPage == totalPage) { firstButton.setEnabled(true); preButton.setEnabled(true); nextButton.setEnabled(false); lastButton.setEnabled(false); } else { firstButton.setEnabled(true); preButton.setEnabled(true); nextButton.setEnabled(true); lastButton.setEnabled(true); } } public PageButtonTableModel getModel() { return model; } public void setModel(PageButtonTableModel model) { this.model = model; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-04-29
现在进入测试
1 建立table的model public class PageModel2 extends PageButtonTableModel { /** * */ private static final long serialVersionUID = 1L; String header[] = { "姓名", "地址", "年龄", "性别", "电话" }; List<Object[]> data = new ArrayList<Object[]>(); public PageModel2() { super(); } public List<Object[]> getData() { return data; } public void setData(List<Object[]> data) { this.data = data; } public int getColumnCount() { return header.length; } public String getColumnName(int c) { return header[c]; } public Object getValueAt(int row, int column) { return data.get(row)[column]; } public boolean isCellEditable(int r, int c) { return false; } public int getRowCount() { if (data != null) { return data.size(); } else { return 0; } } } 2 建立bean public class PageTableBean { String name; String address; int age; String sex; String photo; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } } 3 建立测试页 public class PageFrame2 extends JFrame implements PageInterface{ private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JScrollPane jScrollPane = null; private JTable jTable = null; List listData; // @jve:decl-index=0: String className = "aPage.PageFrame2"; // @jve:decl-index=0: private PageModel2 model; private List<Object[]> tableData; // @jve:decl-index=0: private int totalPage = 20; private JPanel jPanel = null; int rows = 20; /** * This is the default constructor */ public PageFrame2() { super(); tableData = this.getTableList(1, rows); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(700, 600); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJScrollPane(), null); jContentPane.add(getJPanel(), null); } return jContentPane; } /** * This method initializes jScrollPane * * @return javax.swing.JScrollPane */ private JScrollPane getJScrollPane() { if (jScrollPane == null) { jScrollPane = new JScrollPane(); jScrollPane.setBounds(new Rectangle(28, 13, 604, 224)); jScrollPane.setViewportView(getJTable()); jScrollPane .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); } return jScrollPane; } /** * This method initializes jTable * * @return javax.swing.JTable */ private JTable getJTable() { if (jTable == null) { model = new PageModel2(); jTable = new JTable(); model.setData(tableData); jTable.setModel(model); } return jTable; } public List getTableList(int page, int rows) { List list = getList(page, rows); listData = new ArrayList(); if (list.size() > 0 && list != null) { for (Object bean : list) { if (bean instanceof PageTableBean) { PageTableBean pageBean = (PageTableBean) bean; String name = ""; if (pageBean.getName() != null) { name = pageBean.getName(); } String address = ""; if (pageBean.getAddress() != null) { address = pageBean.getAddress(); } int age = 0; if (pageBean.getAge() != 0) { age = pageBean.getAge(); } String sex = ""; if (pageBean.getSex() != null) { sex = pageBean.getSex(); } String photo = ""; if (pageBean.getPhoto() != null) { photo = pageBean.getPhoto(); } Object[] ob = new Object[5]; ob[0] = name; ob[1] = address; ob[2] = age; ob[3] = sex; ob[4] = photo; listData.add(ob); } } } return listData; } public static List<PageTableBean> getList(int page, int rows) { List<PageTableBean> list = new ArrayList<PageTableBean>(); for (int i = page; i < rows + page; i++) { PageTableBean bean = new PageTableBean(); bean.setName("name" + i); bean.setAddress("address" + i); bean.setAge(i); if (i % 2 == 0) { bean.setSex("男"); } else { bean.setSex("女"); } bean.setPhoto("photo" + i); list.add(bean); } return list; } /** * This method initializes jPanel * * @return javax.swing.JPanel */ private JPanel getJPanel() { if (jPanel == null) { jPanel = new JPanel(); jPanel.setLayout(null); jPanel.setBounds(new Rectangle(27, 237, 605, 30)); ButtonPanel bp = new ButtonPanel(totalPage, className, rows, jTable); bp.setBounds(new Rectangle(100, 0, 400, 30)); bp.setModel(model); jPanel.add(bp); } return jPanel; } } 运行ok |
|
返回顶楼 | |
发表时间:2010-04-29
后台ejb的写法如下:
public class AppatatusPageTest implements ApparatusPageTestRemote { @PersistenceContext private EntityManager entityManager; @Override public List<Apparatus> queryApparatus(int whichPage, int pagesize) { // TODO Auto-generated method stub try { String sql = "select a from Apparatus a"; Query query = entityManager.createQuery(sql); // 从结果集中的那个索引开始获取 if(whichPage>=1){ query.setFirstResult((whichPage - 1) * pagesize); } // 设置获取多少条记录 query.setMaxResults(pagesize); List<Apparatus> list = query.getResultList(); //分离内存中受EntityManager管理的实体bean,让VM进行垃圾回收 entityManager.clear(); return list; } catch (Exception e) { e.printStackTrace(); return null; } } public Integer querySumApparatus() { String sql = "select count(*) from Apparatus a"; Query query = entityManager.createQuery(sql); int totalPageSize = Integer.valueOf(query.getResultList().get(0) .toString()); return totalPageSize; } } |
|
返回顶楼 | |
发表时间:2010-05-04
好复杂啊!!!
|
|
返回顶楼 | |
发表时间:2010-05-04
LZ,你的分页也写得太复杂了点吧。。
|
|
返回顶楼 | |
发表时间:2010-05-04
有点复杂了撒。
|
|
返回顶楼 | |
发表时间:2010-05-04
太复杂了。
|
|
返回顶楼 | |