gpt4 book ai didi

java - 如何使用 OOP 概念将 arraylist 对象设置为表

转载 作者:行者123 更新时间:2023-11-30 08:09:13 25 4
gpt4 key购买 nike

我在方法中创建了一个 OrderModel 对象,用于从 DB 获取值并将其设置为 JTable。我想知道如何使用这段代码将这个创建的对象设置为 JTable:

 tblOrderItems.setModel(DbUtils.resultSetToTableModel());

这是我的代码:

private void tblOrderListMouseClicked(java.awt.event.MouseEvent evt) {                                          
int raw = tblOrderList.getSelectedRow();
OrderModel or;
String item;
Double qty,amount,total;

ArrayList<OrderModel> arrOrder = new ArrayList<OrderModel>();

String selectedRaw = tblOrderList.getModel().getValueAt(raw, 0).toString();
String sql = "select item,qty,amount from orderdetails where orderid='"+selectedRaw+"'";
con = new DBconnector().connect();
try {
ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()){
or = new OrderModel();
or.setItem(rs.getString("item"));
or.setQty(rs.getDouble("qty"));
or.setAmount(rs.getDouble("amount"));
arrOrder.add(or);

DefaultTableModel tblModel = new DefaultTableModel();
// wanted to set ArrayList to table+
tblOrderItems.setModel(DbUtils.resultSetToTableModel());

}


} catch (Exception e) {
e.printStackTrace();
}

}

最佳答案

首先创建您自己的 TableModel,它可以管理您的 OrderModel 类,它代表表中的单个行...

public static class OrderTableModel extends AbstractTableModel {

protected static final String[] COLUMN_NAMES = {"Item", "Qty", "Amount"};
private List<OrderModel> rows;

public OrderTableModel(List<OrderModel> rows) {
this.rows = new ArrayList<>(rows);
}

@Override
public int getRowCount() {
return rows.size();
}

@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}

@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
OrderModel row = rows.get(rowIndex);
switch (columnIndex) {
case 0:
value = row.getItem();
break;
case 1:
value = row.getQty();
break;
case 2:
value = row.getAmount();
break;
}
return value;
}

}

然后你可以使用类似...

private void tblOrderListMouseClicked(java.awt.event.MouseEvent evt) {
int raw = tblOrderList.getSelectedRow();
OrderModel or;
String item;
Double qty, amount, total;

ArrayList<OrderModel> arrOrder = new ArrayList<OrderModel>();

String selectedRaw = tblOrderList.getModel().getValueAt(raw, 0).toString();
String sql = "select item,qty,amount from orderdetails where orderid=?";
con = new DBconnector().connect();
try (PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, selectedRaw);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
or = new OrderModel();
or.setItem(rs.getString("item"));
or.setQty(rs.getDouble("qty"));
or.setAmount(rs.getDouble("amount"));
arrOrder.add(or);
}
OrderTableModel model = new OrderTableModel(arrOrder);
tblOrderItems.setModel(model);
}
} catch (SQLException e) {
e.printStackTrace();
}

}

简单地加载 ArrayList 中的项目,然后将其传递给 OrderTableModel 的实例,然后应用于 JTable

看看How to Use Tables有关使用您自己的自定义模型的更多详细信息。

看看Using Prepared Statements有关如何使用 PreparedStatement

的更多详细信息

看看The try-with-resources Statement有关如何更好地管理数据库(和其他)资源的更多详细信息

关于java - 如何使用 OOP 概念将 arraylist 对象设置为表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com