Solved

Jtree question

Posted on 2001-09-11
10
481 Views
Last Modified: 2012-06-27
hi, I have a simple question

if I know a node in jtree how do I get
its index.

I have a node  "addnode" which has three
children.

addnode
 --node 1
 --node 2
 --node 3

when the user selects addNode I want
to get the index of all the children of the selected
node.



i.e I want to get index of node 1,node 2,node 3

I can get index of addnode by
 tree.getLeadSelectionRow();

but how do I get indices of the children?

I tried doing this
 Object arr [] = node1.getUserObjectPath();
     TreePath path = new TreePath(arr);
     int index = dragTree.getRowForPath(path);

but I always get index as -1


Thanks

0
Comment
Question by:sanjay_thakur
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6475811
I didn't understand exactly what you want. For what do you need the indexes and not the childs ?
0
 

Accepted Solution

by:
lawpan earned 25 total points
ID: 6476277
DefaultMutableTreeNode destination = null;
for (int i = 0;i<destination.getChildCount();i++)
{
  int index = destination.getIndex(destination.getChildAt (i));
 System.out.println(index);
}
0
 

Expert Comment

by:lawpan
ID: 6476288
Notice that destination is null.
Replace this with your node.
Beside, if you have 3 children, you always know that indeces are 0,1,2. Or do you want indeces against the whole tree?
0
 

Expert Comment

by:lawpan
ID: 6476301
If you want the indeces relative to the whole tree,
for (int i = 0;i<destination.getChildCount();i++)
{
    TreePath t = new TreePath(((DefaultMutableTreeNode)destination.getChildAt(i)).getPath());
    int index = getRowForPath(t);
    System.out.println(index);
}

You have to typecast the TreeNode to defaultMutableTreeNode
(correction due your example) to get the TreePath
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6477467
Hi,

I tried your solution.
There is a weird problem

If the parent node is not expanded I get the
indices of the child nodes as -1

but If I expand the parent node and then try doing the same
I get the proper values.

I can write code to dynamically expand the
node but I want to know if
this is normal or there is something I am missing

This is the code snippet

selNode = dragTree.                            getLeadSelectionPath().getLastPathComponent();



        System.out.println("selNode " + selNode);
     indexArray = new int[nodeList.size()];

     for (int i = 0;i<selNode.getChildCount();i++)
        {
        TreePath t = new TreePath(((DefaultMutableTreeNode)selNode.getChildAt(i)).getPath());
        System.out.println("t" + t);
        int index = dragTree.getRowForPath(t);
        System.out.println("index" + index);
        indexArray[i] = index;
        }









0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 6478446
> int index = dragTree.getRowForPath(path);

That's not the indice of the the node, that's the displayed row number.
The indices of the child nodes are 0, 1, 2.
0
 

Expert Comment

by:lawpan
ID: 6478690
Yes. You got -1 because
if you tree is
-Node (expanded)
--Child
--Child
-Node2 (collpased with 2 childs also)
-Node3 (empty)

If you are asking the index of child1 of node2 that is NOT visible in tree.(int index = dragTree.getRowForPath(t);
)
You have got -1 since there is no rowForThatPath.

Maximal index in the tree that may be returned is 4 in the example.(0,1,2,3,4 = 5 visible nodes)
And you need to know the index of a node that is not visible.
If you expand node2 then indeces are rearranged. Node index 4(that is child1 of node2) is pointing to a different node in the tree. This means that knowning indeces relativ to the tree dont give you any specific information since the indeces of nodes are changed and repointed to different nodes.
Could you tell my why do you need those indeces. I may understand you problem better and give another soution.
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6480488
okk

I have a node like this

--switch
   --disk 0
   --disk 1
   --disk 2

now there are different volumes associated with these
children
say the disk0--volume 30 gb
        disk1--volume 50 gb
        disk2--volume 60 gb


when the user drags this switch node and drops it
under a drop target node say "target" I want to
add these childern to the "target"
like this
--target
   --disk0--30
   --disk1--50
   --disk2--60


I assumed that indices and row number means the same
but what I need is row numbers of the children
since I read these volumes from an array based on the
row number

ex:row number 2 --volume associated is array[2];

so when a user selects switch and drags it; I need
to know the row numbers of all the children so that
I can accordingly read the volumes from the array

That's the reason I need to know the row numbers
even if the children are not visible


Is it that If I dont expand the node I wont be able to get the row numbers of the children?

I have dynamically expanded the selected node
and it works ok

Just ket me know if there is a better solution to this?









 







0
 

Expert Comment

by:lawpan
ID: 6481790
Hmm
DnD
You have the target, you have the source and its children.
If you setUserObject that is a volume added in disk.
You easy get the userObject from source and add to the target.
I have hard to believe that you need indeces att all.
Only when traversing the parent from source and its easy, as from my very first example. Dont use rowforpaTH.
0
 
LVL 92

Expert Comment

by:objects
ID: 6484017
DefaultMutableTreeNode destination = ...;
for (int i = 0;i<destination.getChildCount();i++)
{
   int index = i;
   System.out.println(index);
}


:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 56
create a gui in perl 3 72
solarwind tftp server 2 45
Unexpected HttpURLConnection connection behavior 2 86
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now