Convert array of values into jTree

Discussion in 'Java' started by groupie, Jun 16, 2008.

  1. groupie

    groupie Guest

    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!
     
    groupie, Jun 16, 2008
    #1
    1. Advertising

  2. groupie

    Roedy Green Guest

    On Mon, 16 Jun 2008 13:58:28 -0700 (PDT), groupie
    <> wrote, quoted or indirectly quoted someone
    who said :

    >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.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 16, 2008
    #2
    1. Advertising

  3. groupie

    groupie Guest

    On Jun 16, 10:23 pm, Roedy Green <>
    wrote:
    > On Mon, 16 Jun 2008 13:58:28 -0700 (PDT), groupie
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    > >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.
    >
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    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.
     
    groupie, Jun 16, 2008
    #3
  4. groupie

    Roedy Green Guest

    On Mon, 16 Jun 2008 14:32:12 -0700 (PDT), groupie
    <> wrote, quoted or indirectly quoted someone
    who said :

    >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.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 17, 2008
    #4
  5. groupie

    groupie Guest

    On Jun 17, 6:48 am, Roedy Green <>
    wrote:
    > On Mon, 16 Jun 2008 14:32:12 -0700 (PDT), groupie
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    > >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.  
    >
    > 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.
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    No just cant crack it - assistance appreciated!!
     
    groupie, Jun 22, 2008
    #5
  6. groupie

    Roedy Green Guest

    On Sun, 22 Jun 2008 04:19:43 -0700 (PDT), groupie
    <> wrote, quoted or indirectly quoted someone
    who said :

    >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.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 22, 2008
    #6
  7. groupie

    groupie Guest

    On Jun 22, 12:57 pm, Roedy Green <>
    wrote:
    > On Sun, 22 Jun 2008 04:19:43 -0700 (PDT), groupie
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    > >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.
    >
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    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.
     
    groupie, Jun 22, 2008
    #7
  8. groupie <> writes:

    > 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
    --
    Lasse Reichstein Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 22, 2008
    #8
  9. groupie

    groupie Guest

    On Jun 22, 3:39 pm, Lasse Reichstein Nielsen <> wrote:
    > groupie <> writes:
    > > 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
    > --
    > Lasse Reichstein Nielsen
    >  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    >   'Faith without judgement merely degrades the spirit divine.'


    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.
     
    groupie, Jun 22, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dmVsdQ==?=

    Convert values into Images.

    =?Utf-8?B?dmVsdQ==?=, Aug 26, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    592
    =?Utf-8?B?dmVsdQ==?=
    Aug 28, 2005
  2. Sam
    Replies:
    1
    Views:
    520
    Thomas Weidenfeller
    Jun 18, 2004
  3. jumpz
    Replies:
    3
    Views:
    634
    Jean-Baptiste Nizet
    Nov 12, 2007
  4. Royan
    Replies:
    32
    Views:
    1,005
  5. Andy B.
    Replies:
    2
    Views:
    1,263
    Alexey Smirnov
    Jun 8, 2009
Loading...

Share This Page