jtree无法获取特定节点的索引

时间:2022-10-27 12:37:34

i have jTree (for example):

我有jTree(例如):

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

when i select springhill i get index of 5, but i dont want to count from the root(paper) (that includes parents and other nodes outside the glossy), i want to start counting from hummermill so i try to get index of 2.

当我选择springhill我得到5的索引,但我不想从根(纸)(包括父母和光泽之外的其他节点)计数,我想从hummermill开始计数所以我试图获得2的索引。

i used tree.getLeadSelectionRow() and get int value of 5; also i use node.getIndex(node) but here i dont get anything normal (0,3,-1). I try all variations of all metods there is for node and tree and find nothing helpfull. plese help!

我使用了tree.getLeadSelectionRow()并获取了5的int值;我也使用node.getIndex(节点),但在这里我没有得到任何正常的(0,3,-1)。我尝试了节点和树的所有方法的所有变体,找不到任何有用的东西。请求帮助!

1 个解决方案

#1


5  

You have to find the index of node from its parent

您必须从其父节点中找到节点的索引

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

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

#1


5  

You have to find the index of node from its parent

您必须从其父节点中找到节点的索引

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

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