我问题是这样的,我从数据库中提出了数据后,用一个hashtable保存,hashtable结构是这样的(id,(parent_id,label)),其中(parent_id,label)也为一个hashtable,现在我要增加一个节点,那我要对新节点进行编号,我是按顺序编的,现在我要得到父节点号,可我对在这样的hashtable中搜到适合我要的节点号不知道怎么办?我用下面程序得到所有数据用数组存贮存,现在找到节点号怎么办
      Hashtable trdata=new Hashtable();
      trdata=(Hashtable)tdata.elementAt(0);//数据取出来
      Enumeration trkeys =trdata.keys() ;
      int num=trdata.size() ;
       String Currlbl[]=new String[num];//保存当年名字
         int   parent_id[]=new int[num];//保存当前父节点号
          int currId[]=new int[num];//保存当前节点号
             int  n=0;
               while (trkeys.hasMoreElements())
                         {
                    Integer  currKey = (Integer) trkeys.nextElement();
                     currId[n]=currKey.intValue();
                         Hashtable testRow = (Hashtable) trdata.get(currKey);
                        Currlbl[n]=testRow.get("trNodeName").toString() ;
                        parent_id[n]=((Integer)testRow.get("trNodepID")).intValue() ;
                      n++;
                       }
                      //对数据进行查找到号
              这里如果我已知树路径,现在我要找到当前树节点在上面的数中节点号,因为我节点名字有可能有的是一样的,所以不同单纯进行比较label,我可以通过TreeNode pathNode[]=selectedNode.getPath() ;得到所选节点的路径,那么我怎么得到当前的节点号。请教,很烦,谁能帮帮我。

解决方案 »

  1.   

    构造树时可以用一个hashtable或者properties保存所有节点信息,把节点对象作为key,节点编号作为value。
    Properties nodesProperty = new Properties();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    nodesProperty.put(root, "root");
    for (int i=0;i<5;i++) {
      DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("node")
      root.add(aNode);
      String nodeValue = "node " + Integer.toString(i);
      nodesProperty.put(aNode, nodeValue);
    }
    //...DefaultTreeModel()...JTree.setModel()...etc以后知道树路径或者树结点的话,从nodesProperty里get一下对应的value就可以了(上面是随手写的代码,所有叶结点名字都是node,对应的value不一样,你所设的value用你的逻辑就行了)