import javax.swing.table.DefaultTableModel;
import java.util.*;
public class RefTableModel extends DefaultTableModel
{
private List lst = new ArrayList(); //对象类集合
private String[] colsName; //列名
private String[] propertiesName; //表中显示的属性定义
private Object[][] contents = new Object[][]{}; //表模型内容
public RefTableModel(String[] colsName, String[] propertiesName) {
this.colsName = colsName;
this.propertiesName = propertiesName;
}
public void init(java.util.List lst) {
this.lst = lst;
this.setDataVector(contents, colsName);
if (lst == null) {
lst = new ArrayList();
}
for (int i = 0; i < lst.size(); i++) {
Object object = lst.get(i);
this.insertRow(i, this.rowObj(object));
}
}
/**
* 通过反射机制取得tableModel每行对应的一条记录
* @param obj - 业务逻辑类实例
* @return Object数组, 数组的每一个值对应一个单元格的值
*/
private Object[] rowObj(Object obj) {
Object[] result = new Object[propertiesName.length]; for (int i = 0; i < result.length; i++) {
|