分页封装2

分享一个封装好的分页实例

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;
}




}

你可能感兴趣的:(sql,C++,c,bean,swing)