Swing Table 分页
package com.table; import java.util.ArrayList; import java.util.List; public class Student { private Integer id; private String name; private String sex; private int age; public static List<Student> students = new ArrayList<Student>(); public Student() { } public Student(Integer id, String name, String sex, int age) { super(); this.id = id; this.name = name; this.sex = sex; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package com.table; import java.util.List; import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; @SuppressWarnings("serial") public class StudentTable extends JTable { // JTable表分页信息相关变量 public int currentPage = 1; public int pageCount = 5; public int totalPage = 0; public int totalRowCount = 0; public int column = 0; public int restCount; public Object[][] resultData; // JTable表信息相关变量 public List<Student> students = Student.students; public String[] columnNames = { "ID", "Name", "Sex", "Age" }; public DefaultTableModel model = null; public StudentTable() { initTable(); } /** * 获取下一页 */ public int getNextPage() { if (this.currentPage != this.totalPage) { return ++currentPage; } return -1; } /** * 获取上一页 */ public int getPreviousPage() { if (this.currentPage != 1) { return --currentPage; } return -1; } /** * 获取最后一页 */ public int getLastPage() { return this.totalPage; } /** * 获取第一页 */ public int getFirstPage() { return 1; } /** * 获取总页数 */ public int getTotolPage() { return this.totalPage; } /** * 获取当前页 */ public int getCurrentPage() { return this.currentPage; } /** * 获得原始数据集 * * @param students * @return */ public Object[][] getData(List<Student> students) { if (students.size() > 0) { Object[][] data = new Object[students.size()][4]; for (int i = 0; i < students.size(); i++) { Student s = students.get(i); Object[] a = { s.getId(), s.getName(), s.getSex(), s.getAge() };// 把List**的数据赋给Object数组 data[i] = a;// 把数组的值赋给二维数组的一行 } return data; } return null; } public void initResultData(Object[][] data) { if (data != null) { resultData = data;// 总的结果集 column = data[0].length;// 表的列数 totalRowCount = data.length;// 表的长度 totalPage = totalRowCount % pageCount == 0 ? totalRowCount / pageCount : totalRowCount / pageCount + 1;// 结果集的总页数 restCount = totalRowCount % pageCount == 0 ? 5 : totalRowCount % pageCount;// 最后一页的数据数 } } /** * 获取分页数据 * * @return */ public Object[][] getPageData() { Object[][] currentPageData = new Object[pageCount][column];// 构造每页数据集 if (this.getCurrentPage() < this.totalPage) {// 如果当前页数小于总页数,那么每页数目应该是规定的数pageCount for (int i = pageCount * (this.getCurrentPage() - 1); i < pageCount * (this.getCurrentPage() - 1) + pageCount; i++) { for (int j = 0; j < column; j++) { // 把结果集中对应每页的每一行数据全部赋值给当前页的每一行的每一列 currentPageData[i % pageCount][j] = resultData[i][j]; } } } else { // 在动态改变数据结果集的时候,如果当前页没有数据了,则回到前一页(一般针对最后一页而言) if (pageCount * (this.getCurrentPage() - 1) >= totalRowCount) this.currentPage--; for (int i = pageCount * (this.getCurrentPage() - 1); i < pageCount * (this.getCurrentPage() - 1) + restCount; i++) { for (int j = 0; j < column; j++) { currentPageData[i % pageCount][j] = resultData[i][j]; } } } return currentPageData; } /** * 初始化表格数据 */ public void initTable() { Object[][] data = getData(students); if (data != null) { initResultData(data); model = new DefaultTableModel(getPageData(), columnNames); } else { // 如果结果集中没有数据,那么就用空来代替数据集中的每一行 Object[][] nothing = { {}, {}, {}, {}, {} }; model = new DefaultTableModel(nothing, columnNames); totalRowCount = 0; } this.setModel(model); this.setRowHeight(20); DefaultTableCellRenderer r = new DefaultTableCellRenderer(); r.setHorizontalAlignment(JLabel.CENTER); setDefaultRenderer(Object.class, r); } }
package com.table; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.table.DefaultTableModel; @SuppressWarnings("serial") public class StudentTableTest extends JFrame implements ActionListener { private JScrollPane panel; private JButton next, previous, add, delete; private JLabel label1; private StudentTable table; public StudentTableTest() { super("表分页及操作"); initTableData(); initComponent(); } private void initTableData() { // TODO Auto-generated method stub Student s = new Student(1, "yangfei", "男", 21); Student.students.add(s); s = new Student(2, "yangf", "女", 22); Student.students.add(s); s = new Student(3, "yangfei", "男", 23); Student.students.add(s); s = new Student(4, "yangf", "女", 24); Student.students.add(s); s = new Student(5, "yangfei", "男", 25); Student.students.add(s); s = new Student(6, "yangf", "女", 26); Student.students.add(s); s = new Student(7, "yangfei", "男", 27); Student.students.add(s); s = new Student(8, "yangf", "女", 28); Student.students.add(s); } private void initComponent() { // TODO Auto-generated method stub this.setSize(500, 230); table = new StudentTable(); panel = new JScrollPane(table); panel.setBounds(10, 10, 470, 119); previous = new JButton("上一页"); previous.setBounds(150, 150, 75, 20); next = new JButton("下一页"); next.setBounds(255, 150, 75, 20); add = new JButton("添加"); add.setBounds(350, 150, 65, 20); delete = new JButton("删除"); delete.setBounds(420, 150, 65, 20); previous.addActionListener(this); next.addActionListener(this); add.addActionListener(this); delete.addActionListener(this); label1 = new JLabel("总共" + table.totalRowCount + "记录|当前第" + table.currentPage + "页"); label1.setBounds(10, 150, 130, 20); this.getContentPane().setLayout(null); this.getContentPane().add(panel); this.getContentPane().add(previous); this.getContentPane().add(next); this.getContentPane().add(add); this.getContentPane().add(delete); this.getContentPane().add(label1); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); } /** * 按钮事件 */ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JButton button = (JButton) e.getSource(); if (button.equals(previous)) { int i = table.getPreviousPage(); if (i == -1) return; } if (button.equals(next)) { int i = table.getNextPage(); if (i == -1) return; } if (button.equals(delete)) { int i = table.getSelectedRow(); if (i == -1) return; Integer id = (Integer) table.getValueAt(i, 0); if (id == null) return; Student s = null; for (Student stu : Student.students) { if (stu.getId().equals(id)) s = stu; } int index = Student.students.indexOf(s); Student.students.remove(index); table.initTable(); label1.setText("总共" + table.totalRowCount + "记录|当前第" + table.currentPage + "页"); return; } if (button.equals(add)) { Integer id = 0; for (Student stu : Student.students) { if (stu.getId() > id) id = stu.getId(); } Student student = new Student(id + 1, "wuming" + (id + 1), "男", 22); Student.students.add(student); table.initTable(); label1.setText("总共" + table.totalRowCount + "记录|当前第" + table.currentPage + "页"); return; } DefaultTableModel model = new DefaultTableModel(table.getPageData(), table.columnNames); table.setModel(model); label1.setText("总共" + table.totalRowCount + "记录|当前第" + table.currentPage + "页"); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new StudentTableTest(); } }