Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Building JTree from strings

 
Mike Foster
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two strings node1/node2/node3/node4" and "node1/node2/node5/node6".... how can I build a ONE JTree in swing from this strings? Here is my Code that builds one string....
 
Tony Docherty
Bartender
Posts: 2989
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

Your node building method needs to take two parameters, the root node (which will be null on the first call) and the String to break into parts to create the nodes.
Once you have split the string you need to do as you are currently doing but when root is not initially null you need to check each split string value against the user object values initially of the root and subsequently of all the children of the node referenced by a node variable (eg called matchNode) which initially points to the root. If you find a node with that value you know you don't need to add a new node so you set the matchNode variable to the node with the matching user object value and check the next split string value, otherwise do as you are currently doing and create a new node. Once you have failed to find a matching child you can stop looking for any more matches for the rest of the input string and just add nodes.
 
Mike Foster
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Welcome to the ranch.

Your node building method needs to take two parameters, the root node (which will be null on the first call) and the String to break into parts to create the nodes.
Once you have split the string you need to do as you are currently doing but when root is not initially null you need to check each split string value against the user object values initially of the root and subsequently of all the children of the node referenced by a node variable (eg called matchNode) which initially points to the root. If you find a node with that value you know you don't need to add a new node so you set the matchNode variable to the node with the matching user object value and check the next split string value, otherwise do as you are currently doing and create a new node. Once you have failed to find a matching child you can stop looking for any more matches for the rest of the input string and just add nodes.


how it would look in code?
 
Tony Docherty
Bartender
Posts: 2989
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I won't write the code for you. But I will help you to write it.

Break down what I have said into small chunks and try to implement them one at a time.
The first part ie adding the method parameters should easy.
The next bit is to create your matchNode variable etc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic