What is wrong with my code?
I'm trying to add a list of paths to a DefaultMutableTreeNode...
I'm not getting the result I expect...can anyone help why?...

public static void main(String [] args){

  

    List lst = new ArrayList<String>();
           lst.add("a/b");
           lst.add("a/b/c/d");
        lst.add("a/b/c");

   DefaultMutableTreeNode node = new DefaultMutableTreeNode("");
  DefaultMutableTreeNode traverser = node;
   

  String currentdata;
   for (String tree : lst){
     DefaultMutableTreeNode traversersparent = null;

      String [] datas = tree.split("/");

      for(int i  = 0; i < datas.length;i++){
          currentdata = datas[i];

        traverser = (DefaultMutableTreeNode)node.getRoot();
        traversersparent = traverser;
        
        while((traverser = traverser.getNextNode()) != null){
            if(!traverser.getUserObject().toString().equals(currentdata)){
                traversersparent.add(new DefaultMutableTreeNode(currentdata));
                break;
              }
            else if(traverser.getUserObject().toString().equals(currentdata))
            {
                i++;
                currentdata = datas[i];
            }
           //   else i++;
        }
          if(traverser == null)
            traversersparent.add(new DefaultMutableTreeNode(currentdata));

       }
       
 }

      JPanel panel = new JPanel();
      MyJtree tree = new MyJtree(node);

        JScrollPane jscrp = new JScrollPane();
        panel.setPreferredSize(new Dimension(500, 500));
        panel.setLayout(new BorderLayout());


        jscrp.getViewport().add(tree);

        jscrp.setBorder(BorderFactory.createTitledBorder("PANEL"));

        tree.expandRow(3); // Expand children to illustrate leaf icons
        DefaultTreeCellRenderer renderer3 = new DefaultTreeCellRenderer();

        tree.setCellRenderer(renderer3);



        panel.add(jscrp);
        JFrame frame = new JFrame();
        frame.setBounds(100,100,500,500);
        frame.getContentPane().add(panel);
        frame.setVisible(true);

 
}
}
This article has been dead for over six months. Start a new discussion instead.