1

I want to get name of each treeitem node and the parents for each, then put them in treemap, but when I run this sample code I get these result as values :

[TreeItem [ value: Function 12 ], TreeItem [ value: Function 13 ]]
[TreeItem [ value: Function 6 ]]
[TreeItem [ value: Function 15 ]]
[TreeItem [ value: Function 9 ], TreeItem [ value: Function 10 ]]

How can I get ride of extra things [TreeItem [ value: ] I just want the string like Function 12

    ArrayList<String> kids = new ArrayList<>();
    TreeMap<String, List<String>> parentChild = new TreeMap<>();
    for (TreeTableColumn<String, ?> tt : treeTable.getColumns()) {

        for (TreeItem node : root.getChildren()) {

            if (!node.isLeaf()) {
                     parentChild.put(node.getValue().toString(),node.getChildren());
            }
        }
    }
    for(Entry<String, List<String>> ent : parentChild.entrySet())
        System.out.println(ent.getValue());
Iman
  • 769
  • 1
  • 13
  • 51

1 Answers1

1

What you are doing is the following:

parentChild.put(node.getValue().toString(),node.getChildren());

You are adding the node.getChildren(), which has the return value of an ObservableList<TreeItem<T>>. And you are adding this to a TreeMap with Value List. You should change your map to

TreeMap<String, List<TreeItem<String>>> myMap = new TreeMap<>(); 

After that you can loop through it later on with:

for(TreeItem node: root.getChildren(){
   parentChild.put(node.getValue().toString(), node.getChildren());
}

for(Entry<String, List<TreeItem>> ent: parentChild.entrySet(){
    for(TreeItem myItem : ent.getValue()){
        System.out.println(myItem.getValue());
    }
}

This should print your "Function X" strings.

NDY
  • 3,527
  • 4
  • 48
  • 63
  • can I get a TreeMap here? – Iman Mar 12 '15 at 10:19
  • Im not sure what you want to achieve with this, but of course you can just hold a TreeMap, List>> and save Parent Node and the children of it. – NDY Mar 12 '15 at 10:27
  • well i want to do this http://stackoverflow.com/q/29007045/4262423 I will try it as you said – Iman Mar 12 '15 at 10:50
  • This the out put {Function 1=[], Function 11=[TreeItem [ value: Function 12 ], TreeItem [ value: Function 13 ]], Function 3=[], Function 4=[], Function 5=[TreeItem [ value: Function 6 ]], Function 7=[TreeItem [ value: Function 15 ]], Function 8=[TreeItem [ value: Function 9 ], TreeItem [ value: Function 10 ]]} – Iman Mar 12 '15 at 10:54
  • One problem is that Function 11 has in fact 3 children for example ( function 13 has a child itself(function 14) which i need to have it as function 11 as well. And I should of course omit TreeItem stuff as you said formerly. – Iman Mar 12 '15 at 10:57