【发布时间】:2017-12-29 00:58:55
【问题描述】:
我在做一个三层的项目,这是来自表示层的代码。我不确定如何使用 arraylist 值填充 jtable,而且我对 tablemodels 了解不多。
table = new JTable();
scrollPane.setViewportView(table);
DefaultTableModel model = (DefaultTableModel) table.getModel();
JButton btnLoadTable = new JButton("Load Table");
btnLoadTable.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Budget_Plan_Controller c = new Budget_Plan_Controller();
ArrayList<Object> totable = c.retrievebudgetdata();
}
});
这是来自实体/数据层的代码,这是为了表明从数据库中检索到的内容正在存储到数组列表中。
public ArrayList<Object> viewbudgetplan(){
ArrayList<Object> budgetdata = new ArrayList<Object>();
try{
Connection connection = sqliteconnection.dbConnector();
String query = "select * from Budget_Plan";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
int id = rs.getInt(1);
String groupid = rs.getString(2);
String groupname = rs.getString(3);
String grouptype = rs.getString(4);
String budgetplanname = rs.getString(5);
double budget = rs.getDouble(6);
double materialcost = rs.getDouble(7);
double transportcost = rs.getDouble(8);
double logisticscost = rs.getDouble(9);
double misccost = rs.getDouble(10);
double totalcost = rs.getDouble(11);
budgetdata.add(id);
budgetdata.add(groupid);
budgetdata.add(groupname);
budgetdata.add(grouptype);
budgetdata.add(budgetplanname);
budgetdata.add(budget);
budgetdata.add(materialcost);
budgetdata.add(transportcost);
budgetdata.add(logisticscost);
budgetdata.add(misccost);
budgetdata.add(totalcost);
}
rs.close();
pst.close();
connection.close();
}catch(Exception e){
e.printStackTrace();
}
return budgetdata;
}
这是来自业务/控制层的代码。该方法仅用于从数据层检索arraylist,它的编写方式是表示层可以调用该方法来检索数据库数据。
public ArrayList<Object> retrievebudgetdata(){
Budget_Plan_Entity e = new Budget_Plan_Entity();
ArrayList<Object> budgetdata = e.viewbudgetplan();
return budgetdata;
}
【问题讨论】:
-
i don't know much about tablemodels.- 有关为 POJO 创建自定义 TableModel 的分步方法,请参阅 Table Row Model。