2016-02-17 28 views
-1

我對如何顯示我的TreeView層次一個問題......對於一個例子,我有這樣的事情....如何顯示我的TreeItem層次[JavaFX的]

    • parent1

      • parent1.1

        • child1

        • 的child2

    • parent2

      • parente2.1

,如果我雙擊的child2我想說明這樣的事情...

根> parent1> parent1.1>的child2

+0

請張貼您的最佳嘗試代碼。謝謝。 – lrnzcig

+0

你在問什麼不太清楚;你是否希望整個樹在雙擊時摺疊成一行?而且,是的,你應該發佈一些代碼來顯示你所嘗試過的。 –

回答

0

獲取的TreeItem層次不是一個困難的任務。簡單地通過反覆要去父節點遍歷樹:

static String createString(TreeItem<String> item) { 
    List<TreeItem<String>> hierarchy = new ArrayList<>(); 
    while (item != null) { 
     hierarchy.add(item); 
     item = item.getParent(); 
    } 

    // list iterator positioned at the end of the list 
    ListIterator<TreeItem<String>> iterator = hierarchy.listIterator(hierarchy.size()); 

    StringJoiner sj = new StringJoiner(" > "); 

    // join Strings in reverse order 
    while (iterator.hasPrevious()) { 
     sj.add(iterator.previous().getValue()); 
    } 
    return sj.toString(); 
} 

使用此展示的項目,當您收到鼠標事件在TreeCell雙擊(不管你用意思是「顯示」)。

+0

謝謝fabian!這正是我正在尋找的! – Rubens

相關問題