wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Building JTree from strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Building JTree from strings" Watch "Building JTree from strings" New topic
Author

Building JTree from strings

Mike Foster
Greenhorn

Joined: Dec 18, 2012
Posts: 2
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

Joined: Aug 07, 2007
Posts: 2170
    
  47
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

Joined: Dec 18, 2012
Posts: 2
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

Joined: Aug 07, 2007
Posts: 2170
    
  47
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Building JTree from strings
 
Similar Threads
JSF 2.0 MyConverter.getAsObject(...) how to get a handle on the backing bean
Table Viewer GUI from a Database
Trying to get manageable arrays to work with
I can't change card in cardLayout using JTree listener
Reference to a String in jni to call a dll function of c++.