给你一个例子:
class SimpleTreeFrame extends JFrame{
  public SimpleTreeFrame(){
    setTitle("SimpleTree");
    setSize(300,200);
    addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });    DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
    DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
    root.add(country);
    DefaultMutableTreeNode state = new DefaultMutableTreeNode("California");
    country.add(state);
    DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose");
    state.add(city);
    city = new DefaultMutableTreeNode("Cupertino");
    state.add(city);
    city = new DefaultMutableTreeNode("Michigan");
    state.add(city);
    city = new DefaultMutableTreeNode("Ann Arbor");
    state.add(city);
    country = new DefaultMutableTreeNode("Germany");
    root.add(country);
    state = new DefaultMutableTreeNode("Schleswig-Holstein");
    country.add(state);
    city = new DefaultMutableTreeNode("Kiel");
    state.add(city);    JTree tree = new JTree(root);
    tree.putClientProperty("JTree.lineStyle","Angled");
    Container contentPane = getContentPane();
    contentPane.add(new JScrollPane(tree));
  }

解决方案 »

  1.   

    ResultSet rs = stmt.executeQuery(query);
    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();
    int rowCount = 1;
    while (rs.next()) {
      DefaultMutableTreeNode(rs.getString(2));//Name字段
    ................//在这里要根据rs.getString(1)的值,即parentId的值 //tree上找到这个结点,然后作为子结点插入其中,查找要通过parentId而不是名字 ,因为parentId唯一。
    rowCount++;
    }
    stmt.close();
    con.close();
    这个问题实际上是从数据库初始化jTree的问题。