向jTree添加多个孩子

时间:2019-02-09 17:22:55

标签: java

我有以下jTree代码,此代码与一个孩子一起工作,然后在下面添加孙子,但是我需要创建一个孩子,然后在下面添加另一个孩子,然后最后添加孙子。

我该怎么做?

 @SuppressWarnings("CallToThreadDumpStack")
public DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
    try {
        int ctrow = 0;
        int i = 0;
        try {

            try {
                con = Database.getConnection();
                stm = con.createStatement();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            String sql = "SELECT catid, catname from category";
            ResultSet rs = stm.executeQuery(sql);

            while (rs.next()) {
                ctrow = rs.getRow();
            }
            String L1Nam[] = new String[ctrow];
            String L1Id[] = new String[ctrow];
            ResultSet rs1 = stm.executeQuery(sql);
            while (rs1.next()) {
                L1Nam[i] = rs1.getString("catname");
                L1Id[i] = rs1.getString("catid");
                i++;
            }
            DefaultMutableTreeNode child, grandchild;
            for (int childIndex = 0; childIndex < L1Nam.length; childIndex++) {
                child = new DefaultMutableTreeNode(L1Nam[childIndex]);
                node.add(child);//add each created child to root
                String sql2 = "SELECT scatname from subcategory where catid= '" + L1Id[childIndex] + "' ";
                ResultSet rs3 = stm.executeQuery(sql2);
                while (rs3.next()) {
                    grandchild = new DefaultMutableTreeNode(rs3.getString("scatname"));
                    child.add(grandchild);//add each grandchild to each child
                }
            }

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

    } catch (Exception e) {
    }

    return (node);
}

0 个答案:

没有答案