Convert array of values into jTree

G

groupie

Hi,
I have the following String array:
PNODE NODE PNAME NNAME DATA1 DATA2 DATA3
0 0 root root - -
-
0 1 root days - - -
0 2 root month - - -
1 3 days even - - -
3 4 even alleven 2nd 4th 6th
1 5 days odd 3rd 5th 7th

I hope you see what I'm getting at (pnode=parent/node=child).
I can't for the life of me figure out how to "convert" the above into
a jTree - I've looked at loads of recursion functions etc. but just
can't seem to get my head around it...convert a String array into a
jTree - any help?

Thanks!
 
R

Roedy Green

0 0 root root - -
-
0 1 root days - - -
0 2 root month - - -
1 3 days even - - -
3 4 even alleven 2nd 4th 6th
1 5 days odd 3rd 5th 7th

First step. Draw a picture the JTree's structure you need to create.
The think about the ORDER the Jtree has to be created. You can't
create a child till its parent is in place.

The basic operaton is adding a node. For that you need a reference to
the parent and the information for the child.

Just build the tree on paper, going down your list, picking off the
information. Then stand back and watch what you are doing so you can
incorporate that is code.

You might temporarily need an aux HashMap lookup of the nodes during
the construction process if you can't build the tree is such a way you
have the parent handy.
 
G

groupie

First step.  Draw a picture the JTree's structure you need to create.
The think about the ORDER the Jtree has to be created. You can't
create a child till its parent is in place.

The basic operaton is adding a node.  For that you need a reference to
the parent and the information for the child.  

Just build the tree on paper, going down your list, picking off the
information.  Then stand back and watch what you are doing so you can
incorporate that is code.

You might temporarily need an aux HashMap lookup of the nodes during
the construction process if you can't build the tree is such a way you
have the parent handy.

Hi,
Thanks for the reply. The structure can change, based on the data in
the array. I can create a jTree normally, i.e. just adding parents/
childs etc.

The data in the string could be anywhere, e.g. there should be childs
of parents scattered all over the array - it's not (currently) sorted
in any order. So what I'm really looking for is an algorithm to
convert the String array data (which has actually been read from a
database table earlier) into a jTree. Thanks.
 
R

Roedy Green

it's not (currently) sorted
in any order

In building a sample list by hand, you may discover that sorting it in
some particular order makes that takes easier.

Your brain is much better at handling specific cases than full
generality. So you solve a number of specific cases, and look for the
general patterns.

You are doing a "Pablo", freaking out at the overall complexity and
hence blocking your ability to solve the problem. You have to do what
you CAN do even if it seems only a futile gesture. That will make you
smarter.

See http://mindprod.com/jgloss/tackling.html
for a generic strategy for handling problems like this.

I'm an old timer. I have been doing this for 45 years. Please try my
suggested strategies before you decide they won't work. Constructing
computer programs is a much messier business than the text books would
let on. You can solve only the simplest problems all at once.
 
G

groupie

In building a sample list by hand, you may discover that sorting it in
some particular order makes that takes easier.

Your brain is much better at handling specific cases than full
generality.  So you solve a number of specific cases, and look for the
general patterns.

You are doing a "Pablo", freaking out at the overall complexity and
hence blocking your ability to solve the problem.  You have to do what
you CAN do even if it seems only a futile gesture. That will make you
smarter.  

Seehttp://mindprod.com/jgloss/tackling.html
for a generic strategy for handling problems like this.

I'm an old timer.  I have been doing this for 45 years. Please try my
suggested strategies before you decide they won't work.  Constructing
computer programs is a much messier business than the text books would
let on.  You can solve only the simplest problems all at once.

No just cant crack it - assistance appreciated!!
 
R

Roedy Green

No just cant crack it - assistance appreciated!!

Others can't help until they understand precisely what you are trying
to do. This is obvious to you but not to me. So please draw a picture.

Show us what the tree would look like that would correspond to your
data.

The other reason to draw a picture is you will have to manually do
what your are expecting the computer to do. That will give you all
manner of clues on how to proceed.
 
G

groupie

Others can't help until they understand precisely what you are trying
to do. This is obvious to you but not to me. So please draw a picture.

Show us what the tree would look like that would correspond to your
data.

The other reason to draw a picture is you will have to manually do
what your are expecting the computer to do.  That will give you all
manner of clues on how to proceed.

Hi,
The tree structure can be derived from the String array values (first
post) where pnode = parent and node = child. PNAME and NNAME have the
descriptions also. So based on data above, tree should be something
like:

root
|-days
|--even
|---alleven
|--odd
|-month

Thanks.
 
L

Lasse Reichstein Nielsen

groupie said:
Hi,
I have the following String array:
PNODE NODE PNAME NNAME DATA1 DATA2 DATA3
0 0 root root - -
-
0 1 root days - - -
0 2 root month - - -
1 3 days even - - -
3 4 even alleven 2nd 4th 6th
1 5 days odd 3rd 5th 7th

I hope you see what I'm getting at (pnode=parent/node=child).
I can't for the life of me figure out how to "convert" the above into
a jTree -

Into a TreeModel, hopefully. The JTree component is just for showing it.
I've looked at loads of recursion functions etc. but just
can't seem to get my head around it...convert a String array into a
jTree - any help?

What you need for a tree model is that a parent knows its children.
What you have is children who know their parent.

So, you need to build the reverse structure. It could be something
like a Map<String, List<String[]>>, that maps a parent's name to a child's
data.

Then run through the arrays and add each to its root's child list.

Preferably, create a class for your objects instead of just working
with String arrays. You can add children to that object too.

From that, a TreeModel is easy to create

Good luck
/L
 
G

groupie

groupie said:
Hi,
I have the following String array:
PNODE  NODE  PNAME  NNAME DATA1 DATA2 DATA3
0            0          root        root       -           -
-
0            1          root       days       -          -          -
0            2          root        month    -          -          -
1            3          days       even      -          -          -
3            4          even       alleven   2nd      4th       6th
1            5          days       odd       3rd       5th       7th
I hope you see what I'm getting at (pnode=parent/node=child).
I can't for the life of me figure out how to "convert" the above into
a jTree -

Into a TreeModel, hopefully. The JTree component is just for showing it.
 I've looked at loads of recursion functions etc. but just
can't seem to get my head around it...convert a String array into a
jTree - any help?

What you need for a tree model is that a parent knows its children.
What you have is children who know their parent.

So, you need to build the reverse structure. It could be something
like a Map<String, List<String[]>>, that maps a parent's name to a child's
data.

Then run through the arrays and add each to its root's child list.

Preferably, create a class for your objects instead of just working
with String arrays. You can add children to that object too.

From that, a TreeModel is easy to create

Good luck
/L

Yes, sorry, I meant a TreeModel. This is what I've done so far:

final int c_pnode = 0;
final int c_node = 1;
final int c_pname = 2;
final int c_nname = 3;
final int c_uname = 4;
final int c_pword = 5;
final int c_info = 6;

DefaultMutableTreeNode root = null;
DefaultMutableTreeNode parent = null;
DefaultMutableTreeNode child = null;

root = new DefaultMutableTreeNode(indata[c_pnode][c_nname]); //root

int ilen = java.util.Arrays.asList(indata).size();

for (int i = 0; i < ilen; i++) { //parent nodes
parent = new DefaultMutableTreeNode(indata[c_pname]);
for (int j = 0; j < ilen; j++) { //get children
if (indata[j][c_pnode].equals((indata[c_pnode]))) {
child = new DefaultMutableTreeNode(indata[j]
[c_nname]);
parent.add(child);
if (i == 0 && j == 0) {
root.add(parent);
}
}
}//for j
} //for i

I'll investigate your suggestion - thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top