2015-06-25 29 views
3

的索引i具有JTree的(例如):JTree的不可能獲得特定節點

-paper 
    -coated 
    -glossy 
     -hummermill 
     -quatro 
     -springhill 
    -matte 

當我選擇春我得到的5的索引,但我不想從根(紙)的計數(包括父母和其他節點以外的光澤),我想從悍馬車開始計數,所以我嘗試索引爲2.

我用tree.getLeadSelectionRow()並得到5的int值;我也使用node.getIndex(node)但在這裏我沒有得到任何正常的(0,3,-1)。我嘗試了節點和樹的所有metod的所有變體,並且找不到任何有用的東西。幫助!

回答

4

你必須從其父

DefaultMutableTreeNode node=(DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent(); 

    System.out.println(node.getParent().getIndex(node)); 
+0

感謝找到節點的索引!這對我來說正是我想要的! –