【问题标题】:Add content of separated Lists to JTable将分隔列表的内容添加到 JTable
【发布时间】:2013-05-16 03:29:33
【问题描述】:

如何使用来自 2 个不同列表的内容填充 JTable? 我有 2 个包含对象的列表,这些对象是从数据库中的 2 个不同表中获得的。 然后我想把它们全部放在 JTable 的一行中。所以我做了 2 个 for 循环,一个添加 list1 的内容,另一个 for 循环添加第二个 List 的内容。

问题是如果数据库中有不止一行数据,第二个列表只是不断重复相同的信息

这是我的代码

private void executeHQLQuery() {
    try {
        Session session = HibernateUtil.getSessionFactory()
            .openSession();
        session.beginTransaction();
        Query q = session.createQuery("from Modelo as m");
        Query q1 = session.createQuery("from Cidade as e");
        List < Modelo > resultList = q.list();
        Integer codModelo = new Integer(resultList.get(0).getCodModelo());
        List < Cidade > resultList2 = q1.list();

        displayResult(codModelo, resultList2, resultList);
        session.getTransaction().commit();
    } catch (HibernateException he) {
        he.printStackTrace();
    }
}
private void displayResult(Integer x, List resultList2, List resultList) {
    Vector < String > nomeTabela = new Vector < String > ();
    Vector tableData = new Vector();
    nomeTabela.add("codModelo");
    nomeTabela.add("codCidade");
    nomeTabela.add("nomeCidade");
    nomeTabela.add("estadoCidade");
    nomeTabela.add("paisCidade");
    nomeTabela.add("nomeModelo");
    nomeTabela.add("anoModelo");
    nomeTabela.add("marcaModelo");
    for (Object o: resultList2) { //Works fine
        Cidade c = (Cidade) o;
        Vector < Object > oneRow = new Vector < Object > ();
        oneRow.add(x);
        oneRow.add(c.getCodCidade());
        oneRow.add(c.getNomeCidade());
        oneRow.add(c.getEstadoCidade());
        oneRow.add(c.getPaisCidade());
        for (Object o1: resultList) {
            Modelo m = (Modelo) o1;
            oneRow.add(m.getNomeModelo());
            oneRow.add(m.getAnoModelo());
            oneRow.add(m.getMarca());

        }

        tableData.add(oneRow);
    }
    resultTable.setModel(new DefaultTableModel(tableData, nomeTabela));
}

resultList2 工作正常,resultList 只是继续显示数据库中最后一个 Modelo 表中的信息

【问题讨论】:

    标签: java swing list jtable


    【解决方案1】:

    看起来您正在执行 2 个查询,但希望其中一个发生变化。

    如果你想让resultList根据resultList2改变,那么你需要把这个查询放在resultList2的循环中。

    Query q1 = session.createQuery("from Cidade as e");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      相关资源
      最近更新 更多