日期:2014-05-20 浏览次数:20914 次
public void showTreeNode(DefaultMutableTreeNode node, int pid){ Connection conn = DB.getConn(); Statement stmt = DB.createStmt(conn); ResultSet rs = DB.executeQuery(stmt, "select nameid, id from u_module where pid="+pid); try { while(rs.next()){ DefaultMutableTreeNode m = new DefaultMutableTreeNode(rs.getString("nameid")); int sid = rs.getInt("id"); if(hasNode(sid)){ showTreeNode(m,sid); } node.add(m); } } catch (SQLException ex) { DB.close(conn); DB.close(stmt); DB.close(rs); ex.printStackTrace(); } DB.close(conn); DB.close(stmt); } public boolean hasNode(int id){ Connection conn = DB.getConn(); Statement stmt = DB.createStmt(conn); ResultSet rs = DB.executeQuery(stmt, "select id from u_module where pid="+id); try { if(rs.next()){ DB.close(conn); DB.close(stmt); return true; } } catch (SQLException ex) { ex.printStackTrace(); } DB.close(conn); DB.close(stmt); return false; } public void init() { if(hasNode(0)){ showTreeNode(root,0); } jf = new JFrame("树"); tree = new JTree(root); //获取JTree对应的TreeModel对象 model = (DefaultTreeModel)tree.getModel(); //设置JTree可编辑 tree.setEditable(true); tree.setRootVisible(false) ;