Jtree was unable to get the index of specific node

I have a jTree (for example):

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

      

when i select springhill i get index 5, but i dont want to count from root (paper) (including parents and other nodes outside gloss), i want to start counting from hummermill so i am trying to get index 2.

i used tree.getLeadSelectionRow()

and got int 5; also i use node.getIndex(node)

, but i'm not getting anything normal here (0,3, -1). I tried all variations of all methods for node and tree and found nothing. Help help!

+3


source to share


1 answer


You need to find the index of the node from its parent



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

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

      

+4


source







All Articles