【问题标题】:How to populate jtable with arraylist values?如何用 arraylist 值填充 jtable?
【发布时间】: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;
}

this is the content inside my database

【问题讨论】:

  • i don't know much about tablemodels. - 有关为 POJO 创建自定义 TableModel 的分步方法,请参阅 Table Row Model

标签: java swing arraylist


【解决方案1】:

首先,我建议使用某种 POJO 而不是像这样的普通对象

public class BudgetTableRowData {

public final String groupid;
public final String groupname;
public final String grouptype;
public final String budgetplanname;
public final double budget;
public final double materialcost;
public final double transportcost;
public final double logisticscost;
public final double misccost;
public final double totalcost;

public BudgetTableRowData(String groupid, String groupname, String grouptype, String budgetplanname, double budget, double materialcost, double transportcost, double logisticscost, double misccost, double totalcost) {
    this.groupid = groupid;
    this.groupname = groupname;
    this.grouptype = grouptype;
    this.budgetplanname = budgetplanname;
    this.budget = budget;
    this.materialcost = materialcost;
    this.transportcost = transportcost;
    this.logisticscost = logisticscost;
    this.misccost = misccost;
    this.totalcost = totalcost;
}

}

现在您可以在 BudgetTableRowData 对象中加载您的数据库行,您可以使用反射在表格中呈现您的数据,这里是一个示例(这样您可以使用它)

public class JtableTut {

public static void showTable(List<Object> data) throws NoSuchFieldException, IllegalAccessException {
    if (!data.isEmpty()) {
        String[] columns = getColumns(data);
        Object[][] dataForTable = getTableData(data, columns);
        JTable table = new JTable(dataForTable, columns);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);

        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(900, 500);
        frame.setVisible(true);
    }

}

private static Object[][] getTableData(List<Object> data, String[] columns) throws IllegalAccessException, NoSuchFieldException {
    Object[][] dataForTable = new Object[data.size()][columns.length];

    for (int i = 0; i < data.size(); i++) {
        for (int column = 0; column < columns.length; column++) {
            String value = data.get(i).getClass().getDeclaredField(columns[column]).get(data.get(i)).toString();
            dataForTable[i][column] = value;
        }
    }
    return dataForTable;
}

private static String[] getColumns(List<Object> data) {
    Field[] fields = data.get(0).getClass().getDeclaredFields();
    String[] columns = new String[fields.length];
    for (int i = 0; i < fields.length; i++) {
        columns[i] = fields[i].getName();
    }
    return columns;
}

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
    List<Object> data = new ArrayList<>();
    // This should be replaced with you logaci for bringing data from db
    for(int i = 0 ; i < 20;i++){
        data.add(getTableRowData());
    }
    showTable(data);
}

private static BudgetTableRowData getTableRowData() {
    Random rand = new Random();
    return new BudgetTableRowData("dsadas", "group1", "creditor", "superBudget", rand.nextDouble(), rand.nextDouble(), rand.nextDouble(), rand.nextDouble(), rand.nextDouble(), rand.nextDouble());
}

}

【讨论】:

    猜你喜欢
    • 2012-01-27
    • 2018-12-14
    • 2015-01-15
    • 2016-01-01
    • 2022-01-10
    • 2012-02-26
    • 2019-10-11
    • 2014-09-09
    • 2016-01-21
    相关资源
    最近更新 更多