将两个ArrayLists添加到JTree

时间:2016-03-16 21:43:49

标签: java swing arraylist while-loop jtree

除了if语句之外,一切似乎都运行良好, 代码中的注释解释了大部分内容。

目的是将两个ArrayLists添加到Jtree。 Arraylist包含整数,如(1,2,3,4等),第二个包含双数,如(1.1,1.2,2.1等)。

我想将第一个数组添加到JTree中,我已经设法做到了。但我想添加第二个arraylist,以便它是第一个的孩子。

因此1.1和1.2是1的孩子,2.1是2的孩子等。

任何帮助都将不胜感激。

    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);


        }
    });

0 个答案:

没有答案