0

everything seems to run fine apart from the if statement, comments in code explain most of it.

The aim is to add two ArrayLists to a Jtree. Arraylist containing whole numbers like (1,2,3,4 etc) and the second containing double number like (1.1,1.2,2.1 etc).

I want to add the first array to the JTree, which i have managed to do. But i then want to add the second arraylist so thats it is a child of the first.

So that 1.1 and 1.2 are a child of 1 and 2.1 is a child of 2 etc.

Any help would be appreciated.

    DisplayTree.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            JFrame frameG2 = new JFrame("Cell Tree");
            frameG2.setSize( 400, 900 );
            frameG2.setVisible(true);

            frameG2.setBackground( Color.gray );

            DefaultMutableTreeNode root = new DefaultMutableTreeNode("cells");

            int i=0;
            //should through first array list and adds it to root
            for (int n =0; n<arrayList.size();n++) {
                DefaultMutableTreeNode cells = new DefaultMutableTreeNode(arrayList.get(n));

                root.add(cells);

                //should go through jtree elements
                Enumeration search = root.postorderEnumeration();
                while(search.hasMoreElements()){


                    //should compare each element to a 2nd array
                    //2nd array consists of double numbers like 1.1,1.2,2.1 etc
                    //so i split it before the "." so 1.1 is 1
                    //first array consists of whole numbers like 1, 2, 3
                    //want to make 1.1 child of 1 etc.
                    if (search.nextElement().toString()==(arrayList2.get(i).toString().split("\\.", 0)[0])) {
                        DefaultMutableTreeNode NewCells = new DefaultMutableTreeNode(arrayList2.get(i));
                        cells.add(NewCells);

                        i++;

                    }else{
                    }

                }
            }


                tree = new JTree(root);
                frameG2.add(tree);


        }
    });
Keags
  • 1
  • 3
  • 1
    Please post all code here with your question as code-formatted text, not in links. – Hovercraft Full Of Eels Mar 16 '16 at 21:44
  • Questions with more focus are more likely to be answered than "please debug my code" proposals. What are you trying that is failing? I suggest you edit your question to lure more readers – Martin Cowie Mar 16 '16 at 21:48
  • `==` (`if (search.nextElement().toString()==(arrayList2.get(i).toString().split("\\.", 0)[0])) {`) should not be used to compare `String` – MadProgrammer Mar 16 '16 at 21:51
  • The part that is failing is the if statement, which is supposed to add the 2nd array to the jtree – Keags Mar 16 '16 at 21:51
  • == or .equals() does not work, get an error with the if statement with equals – Keags Mar 16 '16 at 21:56

0 Answers0